Category Archives: Linux Administration

Bash scripting for RSync logging

The following bash script is used to email a snapshot of the rsync job status upon completion of the job:


#!/bin/sh

RSYNC=/usr/bin/rsync
SSH=/usr/bin/ssh
KEY=/home/rsync/cron/key
RUSER=rsync
RHOST=portal.rhost.com
RPATH=/media/DOC_CONTROL/
LPATH=/rhost/DOC_CONTROL
LOGFILE=/home/rsync/rsync.log

$RSYNC -azhh --stats --log-file=$LOGFILE -e "$SSH -i $KEY" $RUSER@$RHOST:$RPATH $LPATH

EMAIL=user@rhost.com
BODY=/home/rsync/email-body.tmp

if [ $? -eq 0 ]
then
STATUS=SUCCESS
else
STATUS=FAILURE
fi

echo -e "Backup Job Summary:\n" > $BODY
/usr/bin/tail -14 /home/rsync/rsync.log >> $BODY
echo -e "\nFile System Usage\n" >> $BODY
/bin/df -h | head -1 >> $BODY
/bin/df -h | grep startllc >> $BODY
echo -e "\n----END LOG----" >> $BODY

/bin/mail -s "$STATUS : Document Control Backup" "$EMAIL" < $BODY

and the email notification looks like:


From: xxxxx
Sent: xxxxx
To: xxxxx
Subject: SUCCESS : Document Control Backup

Job Summary:

2015/02/26 14:13:28 [7170] Number of files: 3282
2015/02/26 14:13:28 [7170] Number of files transferred: 0
2015/02/26 14:13:28 [7170] Total file size: 5.91G bytes
2015/02/26 14:13:28 [7170] Total transferred file size: 0 bytes
2015/02/26 14:13:28 [7170] Literal data: 0 bytes
2015/02/26 14:13:28 [7170] Matched data: 0 bytes
2015/02/26 14:13:28 [7170] File list size: 98.46K
2015/02/26 14:13:28 [7170] File list generation time: 0.002 seconds
2015/02/26 14:13:28 [7170] File list transfer time: 0.000 seconds
2015/02/26 14:13:28 [7170] Total bytes sent: 352
2015/02/26 14:13:28 [7170] Total bytes received: 99.74K
2015/02/26 14:13:28 [7170] sent 352 bytes received 99.74K bytes 28.60K
bytes/sec
2015/02/26 14:13:28 [7170] total size is 5.91G speedup is 61895.84
2015/02/26 14:13:28 [7168] sent 353 bytes received 102155 bytes total size
6343704911

File System Usage

Filesystem Size Used Avail Use% Mounted on
/dev/sdb1 2.7T 1.4T 1.2T 55% /rmount

----END LOG----

Xorg.conf for dual head fglrx

I’m running two monitors on an ATI HD5770 card and want a single spanned desktop. Unfortunately, the two monitors have different resolutions — the primary HP ZR24W runs at 1900 x 1200 while the secondary Dell 2208WFP runs at 1680 x 1050. This is enough to throw the ATI installer for a loop. Not to mention that the Catalyst Control Center crashes on apply on my Fedora 14 install.

The manual procedure for setting up the system is:

aticonfig --initial=dual-head --screen-layout=right --output=/etc/X11/xorg.conf

This gets me to a true dual-head configuration with two independent desktops. However, since we want a spanned desktop, we need to turn on Xinerama. This is achieved by add ing the line:

Option "Xinerama" "on"

to the Section “ServerLayout” area of the xorg.conf.

The full working xorg.conf is:


Section "ServerLayout"
        Identifier     "aticonfig Layout"
        Screen      0  "aticonfig-Screen[0]-0" 0 0
        Screen         "aticonfig-Screen[0]-1" RightOf "aticonfig-Screen[0]-0"
        Option  "Xinerama"      "on"
EndSection

Section "Module"
EndSection

Section "Monitor"
        Identifier   "aticonfig-Monitor[0]-0"
        Option      "VendorName" "ATI Proprietary Driver"
        Option      "ModelName" "Generic Autodetecting Monitor"
        Option      "DPMS" "true"
EndSection

Section "Monitor"
        Identifier   "aticonfig-Monitor[0]-1"
        Option      "VendorName" "ATI Proprietary Driver"
        Option      "ModelName" "Generic Autodetecting Monitor"
        Option      "DPMS" "true"
EndSection

Section "Device"
        Identifier  "aticonfig-Device[0]-0"
        Driver      "fglrx"
        BusID       "PCI:1:0:0"
EndSection

Section "Device"
        Identifier  "aticonfig-Device[0]-1"
        Driver      "fglrx"
        BusID       "PCI:1:0:0"
        Screen      1
EndSection

Section "Screen"
        Identifier "aticonfig-Screen[0]-0"
        Device     "aticonfig-Device[0]-0"
        Monitor    "aticonfig-Monitor[0]-0"
        DefaultDepth     24
        SubSection "Display"
                Viewport   0 0
                Depth     24
        EndSubSection
EndSection

Section "Screen"
        Identifier "aticonfig-Screen[0]-1"
        Device     "aticonfig-Device[0]-1"
        Monitor    "aticonfig-Monitor[0]-1"
        DefaultDepth     24
        SubSection "Display"
                Viewport   0 0
                Depth     24
        EndSubSection
EndSection

Using ‘sed’ to repace text in a file

I have 100 .svg files in a directory that I wanted to crop by one pixel on each side. This was an automatically generated 1 px wide white border, so it was controlled by only a single line in the .svg file specifying a white box from 0,0 to 31,31. The hard way would be to use Inkscape to modify each file individually, but once again ‘sed’ can come to the rescue and modify all 100 files with one command.

The pertinent line that I needed to change was

<rect x=”0″ y=”0″ width=”31″ height=”31″ fill=”#FFFFFF” />
to
<rect x=”1″ y=”1″ width=”29″ height=”29″ fill=”#FFFFFF” />

so we invoke ‘sed’ with


# sed -i 's/x="0" y="0" width="31" height="31"/x="1" y="1" width="29" height="29"/1' *.svg

The ‘-i’ switch tells ‘sed’ to modify the files in place and the ‘s/regexp/replacement/’ defines the string to search for and replace. the ‘/1′ switch on the end tells ‘sed’ to operate only on the 1st instance of the matched expression.

Leave out the ‘-i’ switch and operate on a single file to preview the changes to stdout before committing.

And voila! 100 files fixed in no time!

Mount ISO disk image

I’m partial to FTP installs of all my linux OS’s. This is mainly due to buggy DVD writers causing me to generate a ton of silver coasters and to generally increase my stress levels. So I;ve been downloading ISO’s and mounting them under my FTP site to allow for network installations. This also seems to run quicker than from disk in some instances since my network throughput is greater than my DVD throughput on some of my laptops. This also helps for systems which do not have cd/DVD drives installed and installation is performed using a USB stick.

So without further ado… To mount an ISO run the following command as root:

mount -r -t iso9660 -o loop /source/disk.iso /target/directory/

Using ‘sed’ to bulk edit text files

Google Analytics provides free yet super-sophisticated website tracking and statistics. Unfortunately, it requires that the following code snippet be inserted into every web page immediately before the </body> tag.


<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-12232036-2");
pageTracker._trackPageview();
} catch(err) {}</script>

Now with a static website such as www.elizabethpassela.com, that’s a lot of pages to manually update. What we’d like is a way to perform a bulk insert of the required text on all .htm files in the webroot directory. Here’s where ‘sed’ comes to the rescue.

First task is to create a ‘sed’ script file called sed.cmd. The first line includes the search string </body> and the /i “insert” option which tells ‘sed’ to insert the following text before the </body> string. The ” at the end of each line is needed to tell ‘sed’ that another line exists in the script, sort of a continuation operator. Since the ‘/’ character was used as the separator for the script command, all ‘/’ characters which are part of the operand need to be escaped with ”.


/</body>/i
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-12232036-2");
pageTracker._trackPageview();
} catch(err) {}</script>

Finally, execute the following command to insert the text into all .htm files. The '-i' tells sed to operate on the files in-place (if 'sed' is run without the '-i' option, the output will be sent to stdout which is good for testing).


# sed -i -f ~cbattles/sed.cmd *.htm

A similar approach can be used for replacing and appending text as well as inserting. Additionally, the 'find command can be piped to 'sed' to operate on selective files.


Caveat lector — All work and ideas presented here may not be accurate and should be verified before application.

Gnuplot + Imagemagik for Web Graphs

To generate the Taylor series graph shown in this post, the base code was generated using Gnuplot with the latex terminal option. The following Gnuplot input file was used to create the code:


set term latex 
set output "graph.tex"
set xrange [-1:3]
set yrange [-2:10]
set key off
set xtics 0
set ytics 0
set border 0 0 
set xzeroaxis
set yzeroaxis
set xtics axis out ("" -0.5, "0.5" 0.5, "" 1, "" 1.5, "" 2, "" 2.5)
set ytics axis out ("" -1, "" 1, "" 3, "" 5, "" 7, "" 9) 
set label 1 " n = 0 " at 2.35, 1.6, 0 
set label 2 " n = 1 " at 1.9, 7, 0
set label 3 " n = 2 " at 1.6, 9.6, 0 
set label 4 " n = 3 " at 2.7, 7.2, 0
set label 5 " 5x^2 - x^4 " at 1.5,3,0  
show label
set title "1st Four Terms of the Taylor Series Expansion of $f(x) = 5x^{2} - x^{4}$" 
show title
plot 5*x**2 - x**4 lt 3 lw 2, 5*0.5**2-0.5**4 lt 4 lw 1, 5*0.5**2-0.5**4+(x-0.5)*(10*0.5-4*0.5**3) lt 4 lw 1, 5*0.5**2-0.5**4+(x-0.5)*(10*0.5-4*0.5**3)+(((x-0.5)**2)/2)*(10-12*0.5**2) lt 4 lw 1, 5*0.5**2-0.5**4+(x-0.5)*(10*0.5-4*0.5**3)+(((x-0.5)**2)/2)*(10-12*0.5**2)+(((x-0.5)**3)/6)*(0-24*0.5) lt 4  lw 1

Linestyles are limited in the latex terminal, but the beauty of this method is that any mathematical formulae can now be rendered in . The output only includes the {picture} block, so the following lines need to be prepended to the output using your favorite editor:

documentclass[10pt]{book}
usepackage{amsmath}
usepackage{amsfonts}
usepackage{amssymb}
usepackage{graphicx}
begin{document}

And finally

end{document}

should be appended.

Any editor may be used to render the output. I used Texmaker 1.9.1 as it was the default installed application on my workstation. Output rendering was to PDF and converted to PNG using ImageMagik by the command:

convert -density 288 graph.pdf -resample 144 -trim +repage graph.png

This ports the PDF through Ghostscript and resizes it from 72dpi to 144dpi while providing some anti aliasing. Final touch ups were performed with the GIMP and the image was posterized to 8 color levels using a level of 3 to reduce the size. Final image size is 9.5k.

The pstricks terminal may be able to do better. Another option is the epslatex terminal which combines postscript output with rendering.

Caveat lector — All work and ideas presented here may not be necessarily provide the same results for another user. The methods appearing here happen to work for me.

Changes on setting up WordPress

Firstly,  I’d like to congratulate the authors on a extremely simple install process.  I’ve tried other Tomcat based CMS software and it was always an exercise in futility to keep them up and running properly.  PHP in the LAMP architecture seems to be the way to go.

Since I was setting this up on my own server, I decided to use a trick that keeps file management easier for me and to install WP to my home directory and simply mount it under the /var/www/html/webroot directory.

As root…


# mount --bind /var/www/html/webroot/blog/ /home/username/wordpress

and add

/var/www/html/webroot/blog/ /home/username/wordpress none bind 0 0

to /etc/fstab