Sunday, November 18, 2012

Postfix Fun

I've had some interesting times with Postfix recently and I thought I'd write a post about it.

Because I have a dynamic IP address, what I wanted to do was two things:
  • set up Postfix to relay email to recipients via my ISP
  • send mail using my ISP domain, instead of localhost.localdomain.
For the first part, I used the following reference:

Set Up Postfix For Relaying Emails Through Another Mailserver

But as I'm using Korora 17, the Postfix service is governed by systemd (not init.d).So to restart the Postfix service, I had to do the following:

# systemctl restart postfix.service

The second issue is solved by editing the Postfix config file and the Hosts file:  

1.Edit the Hosts file:

# vim /etc/hosts

127.0.0.1       exampleisp.com  localhost.localdomain localhost
::1             localhost6.localdomain6 localhost6

2. Send an email from your mail server and check the log:

# tail -f /var/log/maillog


3. Edit Postfix config file: 

# vim /etc/postfix/main.cf

Insert your ISP's domain (in this example, I'm using: exampleisp.com) as follows:

mydomain = exampleisp.com
myhostname = mail.exampleisp.com
myorigin = user@exampleisp.com
relayhost = [mail.exampleisp.com]
inet_interfaces = all 

alias_maps = hash:/etc/aliases

Make sure you restart the Postfix service:
# systemctl restart postfix.service

Note: the alias_maps entry gets rid of the following warning: dict_nis_init: NIS domain name not set - NIS lookups disabled

4. Test by sending an email to an external email address:

$ mail someone@gmail.com
Subject:
[body text]
.
EOT


For some good background information, see: http://www.postfix.org/BASIC_CONFIGURATION_README.html

Friday, September 7, 2012

Connecting a webcam to Zoneminder

This post follows on from my last post on installing Zoneminder and describes how to connect your webcam.


1. Test the webcam. To do this, I used Kamoso (shipped with Kororaa).

2. Capture the webcam settings. To do this, I created an empty file called webcam_settings and then directed the settings output to that file:

sudo zmu -d /dev/video0 -q -v > ~/Documents/webcam_settings

My output is shown below:

Video Device: /dev/video1
General Capabilities
  Driver: uvcvideo
  Card: HD Webcam C615
  Bus: usb-0000:00:1d.7-1.4
  Version: 3.5.2
  Type: 0x4000001
    Supports video capture (X)
    Does not support video output
    Does not support frame buffer overlay
    Does not support VBI capture
    Does not support VBI output
    Does not support sliced VBI capture
    Does not support sliced VBI output
    Does not support video output overlay
    Does not have tuner
    Does not have audio in and/or out
    Does not have radio
    Does not support read/write i/o (X)
    Does not support async i/o
    Supports streaming i/o (X)
    Standards:
  Formats:
    YUV 4:2:2 (YUYV) (YUYV)
    MJPEG (MJPG)
Crop Capabilities
  Bounds: 640 x 480
  Default: 640 x 480
Inputs: 1
  Input 0
    Name: Camera 1
    Type: Camera
    Audioset: 00000000
    Standards: 0x0
    Power on  (X)
    Signal detected  (X)
    Colour Signal detected
    Horizontal Lock detected 

3. Open a web browser and go to localhost/zm

Result: You should see the Zoneminder console and the status should be running.


4. Click Add a New Monitor and enter the relevant setting values into the video related options of your webcam (from step 2).

Note: I have included some screenshots of my config below but for more detail on the various settings, see the Zoneminder wiki (http://www.zoneminder.com/wiki/index.php/Documentation#Defining_Monitors)

4. Once you have finished entering your settings, click Save.
5. To see a live video feed, click the monitor name.

Thursday, August 30, 2012

Installing Zoneminder on Kororaa 17

I wanted to setup a motion-activated webcam at home. I read some good things about Zoneminder, so I thought I'd give it a go. This post describes how I set up Zoneminder on my Kororaa 17 (KDE) machine. If I can get things working OK, I'll write some follow-up posts on connecting the camera and getting Zoneminder to send alerts.

Note: The Zoneminder website has instructions for installing it on Fedora 17 (see: http://www.zoneminder.com/wiki/index.php/Fedora and http://www.howtoforge.com/installing-apache2-with-php5-and-mysql-support-on-fedora-17-lamp). The main difference in my case is that Kororaa already comes with mysql and mysql-server installed.

1. Install Zoneminder via your package manager:

$ su -  
# yum install zoneminder.i686

2. Setup mySQL:

# systemctl enable mysqld.service  
# systemctl start mysqld.service
# mysql_secure_installation

Follow the instructions on the console:
  1. Enter - no password currently set
  2. Enter password
  3. Confirm password
  4. Enter - Disable anonymous user
  5. Enter - Disallow root login remotely
  6. Enter - Remove test database
  7. Enter - Reload privileges table
3. Build the database:

# mysql -u root -p < /usr/share/zoneminder/db/zm_create.sql

4. Grant privileges to zmuser:

Start the mySQL monitor:

# mysql -u root -p

Grant privileges:
mysql> grant select,insert,update,delete on zm.* to 'zmuser'@localhost identified by 'zmpass';

Switch to zm database and quit:
mysql> use zm;  
mysql> quit

5. Edit the Apache config file:
# vi /etc/httpd/conf.d/zoneminder.conf

Remove the line: Deny from all # DELETE THIS LINE

Start (or restart) Apache:
# systemctl start httpd.service

6. Start Zoneminder:
# service zoneminder start

Note: If you are running Selinux in enforcing mode, refer to these instructions: http://www.zoneminder.com/wiki/index.php/Main_Documentation#Configuring_SELinux_Policy. I followed the instructions but still got an error trying to run the Zoneminder service. I was able to start it by setting SELinux Enforcing Mode to Permissive.

Result: If you go to http://localhost/zm/, you will see the Zoneminder console.







Friday, August 24, 2012

Capturing a network stream with VLC

This post describes how I use VLC to record a network stream. I had so much trouble finding an answer to this problem I thought it was worth posting something about it.

Note: These instructions are based on VLC for Linux and focus on the GUI, not command line. The specific distro I'm using is Kororaa.
  1. First, you need the get the details of the network stream you want to capture (for example, http://example.com/stream.avi). If you have an m3u playlist file, the file properties will give you this information. 
  2. Open VLC. 
  3. Select Media - Open Network Stream
  4. In the Please enter a network URL field, enter the stream location from Step 1. 
  5. From the Play options drop-down list, select Convert
  6. Result: The Convert window is displayed.
  7. In the Destination field, name the output file and select where you want it saved:
    • Navigate to the save location.
    • In the Name field, type the filename (for example, funk.mp3).
    • Deselect the Automatically select filename extension checkbox.
    • Click Save
  8. In the Settings section, you can either select the output file type you want, or check the Dump raw input checkbox.
  9. Click Start. 
  10. Result: Happy days! VLC starts streaming and the output is saved in the location you selected in Step 6.

Tuesday, November 23, 2010

Creating files for testing - part 2

To test the behaviour of one of our applications, I needed to create a certain number of files of a certain file format. To do this, I wanted to copy the source file (a bitmap) multiple times and have the result go to the same directory. So the files did not overwrite each other, I needed to increment the file name (like: 0file.bmp, 1file.bmp, 2file.bmp).

With some help from one of the developers, I came up with a script that can also create a specified number of text files (file1.txt, file2.txt etc.)

The script takes two inputs:
  • number of files to create - mandatory
  • file to copy - optional

If a file to copy is not specified, the script will create as many text files as the mandatory number entered.

I've published the script on our team's repository on GitHub. You can find it here:

https://github.com/csmart/naa/blob/master/sysadmin/scripts/filecreator.sh

Creating files for testing - part 1

Sometimes I need to create files of a certain size for testing purposes. Using the Linux 'dd' command, this is pretty easy.

There are lots of other posts around on how to do this. This is one of them on 'dd', including how to create the file of a certain size:


http://www.linuxprogrammingblog.com/dd-as-a-swiss-army-knife


I've noticed that some people suggest creating a file like this:

dd if=/dev/zero of=test-file bs=1M count=2

This produces a 2MB file.

and others, like this:

dd of=test-file bs=1M seek=2 count=0 (uses stdout as input)

dd if=/dev/zero of=test-file bs=1M seek=2 count=0 (uses /dev/zero as input)

Both of the above create a 2MB file.

Using 'seek' creates a sparse file.

Thursday, September 16, 2010

Bulk edits of documents with Sed

I recently had a need to edit a number of html documents to remove a section of html code. Doing a bit of a search, it seemed that the best way of doing this was via SED (Stream EDitor). Sed is used in Linux to filter or transform text.

The next thing I wanted to do was to find out how I could use sed to:
  1. Find a string within an html file.
  2. Delete the string.
  3. Do this for more than one html.
The first thing I had to do was to create a bash script with a for loop that can cycle through the html files one by one.

The script I wrote was based on the following (I got this idea from here: http://gabeanderson.com/2008/02/01/unixlinux-find-replace-in-multiple-files/).

for fl in *.php; do
mv $fl $fl.old
sed ’s/FINDSTRING/REPLACESTRING/g’ $fl.old > $fl
#rm -f $fl.old
done


In my case I want to delete the string and I'm dealing with .html files, so the first line of my script becomes:

for fl in *.html; do

The second line remains the same:

#renames the .html files to .html.old

mv $fl $fl.old


The string I wanted to delete was multiple lines of html. I decided to specify the start and end of the string. Also, instead of replacing text, I just want to delete it, so I change the 'g' to a 'd'.

My third line becomes:

#this deletes all the html between the <h4> tag and the close table cell tag and redirects the output to .html

sed ’/<h4>/,/<\/td>/d’ $fl.old > $fl

I didn't want to keep the .old files so I uncommented the fourth line:

rm -f $fl.old #deletes the .html.old files

So, the script ends up looking like this:


#!/bin/bash

# script to replace multiple lines of text in multiple html files

for fl in *.html; do
mv $fl $fl.old
sed '/<h4>Are you/,/<\/td>/d' $fl.old > $fl
rm -f $fl.old
done


To run it, I just had to make it executable and run it from within the directory containing the .html files.