conky script used to monitor server status remotely.

A typical Conky look
Image via Wikipedia

I was looking for something to monitor few details on the server. I thought about quite a lot of applications, some open source and some scripts developed in house. But my requirements were quite petty and the scripts and applications were quite heavy. So, I thought why not conky :).

 

So, with some quick work on conky script, I was able to get what I wanted and here it is for all of you.

This is a bash script, that will create the conky script and then start conky.

#!/bin/bash - 
#===============================================================================
#
#          FILE:  conky.sh
# 
#         USAGE:  ./conky.sh 
# 
#   DESCRIPTION:  Start conky to display who on all the servers.
# 
#       OPTIONS:  ---
#  REQUIREMENTS:  ---
#          BUGS:  ---
#         NOTES:  ---
#        AUTHOR: Amit Agarwal (aka), amit.agarwal@roamware.com
#       COMPANY: Roamware India Pvt Ltd
#       CREATED: 12/21/2011 02:06:33 PM IST
# Last modified: Wed Dec 21, 2011  05:56PM
#      REVISION:  ---
#===============================================================================
echo killall conky processes
kill -9 $(ps -eaf|grep conkyrc|awk '{print $2}'|tr '\n' ' ')
conkyrc=~/.conkyrc.who
cat <$conkyrc
background no
alignment top_right
update_interval 50
double_buffer
short_units yes

#colors
#color black
#color0 ff6633
#color1 ffffff
#color2 cccccc
#color3 ffff66

#xft settings
use_xft yes
xftfont arial:size=10

cpu_avg_samples 2
net_avg_samples 2
#window settings
own_window yes
own_window_transparent no
own_window_colour black
#own_window_type
own_window_hints undecorated, sticky, skip_taskbar, below

#spacing
gap_x 0
gap_y 0
alignment top_left
#colors
color0 ffffff
color1 00FF97
color2 FF6D00
color3 FE003C
color4 FD9C00
color5 FDF700
color6 9101FE
color7 A2FF00
color8 FE003C
color9 000000

TEXT
EOF
count=1
for i in 1.1 1.2  1.3
do

    line1="$line1\${color$count}172.16.$i      "
    line2="$line2 \${color$count}Count : .....\${execi 50 ssh user@172.16.$i \"who\"|wc -l}.. "
    line3="$line3 \${color$count}..\${execi 50 ssh user@172.16.$i \"who\"|sed \"s/[    ].*//\" |sort |uniq -c|sort -nr|head -1|tr '\t' ' '|sed 's/ \\{2,\\}/ /g'}"
    line4="$line4 \${color$count}Processes:\${execi 50 ssh user@172.16.$i ps -fu user|wc -l}"
    ((count++))
done 
echo $line1 >>$conkyrc
echo $line2 >> $conkyrc
echo $line3 >> $conkyrc
echo $line4 >> $conkyrc
conky -c $conkyrc  &
Enhanced by Zemanta

Backup of files in the directory.

I was working on some scripts and the changes that I was making in the scripts was very dynamic, which I did want to keep backing up in the version control system. But for the peace of my mind, I wanted to keep a copy of the scripts, whenever it was in working state.

Since I had multiple files, so it would make more sense to have a script that could copy all the files in the current directory to “old” directory without over-writing the existing files. So, I wrote a script that would postfix the files with a number. With this approach, finally what I had was the following:

#!/bin/bash -
#===============================================================================
#
#          FILE:  backup.sh
#
#         USAGE:  ./backup.sh
#
#   DESCRIPTION:  Backup all the current files.
#
#       OPTIONS:  ---
#  REQUIREMENTS:  ---
#          BUGS:  ---
#         NOTES:  ---
#        AUTHOR: Amit Agarwal (aka), amit.agarwal@roamware.com
#       COMPANY:
#       CREATED: 08/19/2011 02:43:32 PM IST
#      REVISION:  ---
#===============================================================================

count=$(cat old/count)
if [[ ! -f old/count ]]
then
  count=$(ls -1 old |sed 's/.*\.//'|grep -vi "[a-z]"|sort|tail -1)
fi
((count++))
echo $count >old/count
[[ ! -d old ]] && mkdir old
for i in *
do
	[[ -f $i ]] && cp $i old/${i%%.*}.$count
done
echo "Backed up to $count"
Enhanced by Zemanta

Compare files excluding certain lines.

Quick tip, you can use any expression for the sed commands in the (). With this trick you can redirect the stdout of 2 commands to the diff command. This might become very useful, if you want to compare 2 files, excluding the firstĀ  line.

diff <(sed '1d' file) <(sed '1d' file2)

More interesting example is where the string ABC is converted to abc before comparing in the second file with the following command:

diff file <(sed 's/ABC/abc/g' file2)
Enhanced by Zemanta