stevepedwards.com/DebianAdmin linux mint IT admin tips info

Introduction to Scripts

http://linuxcommand.org/lc3_writing_shell_scripts.php

------------------------------------------------------------------------------------

As there seems no way to send an email of the ClamAV report from within Clam packages themselves, I googled and found only scripts as the solution, usually in conjunction with cron, where periodic cron jobs are scheduled to run a scan, then email the report out.

Not being a programmer I shied away from this topic in the past, but as I now have a functional Exim mail client, I looked at other useful tasks that could be emailed and modified to suit Clam reports, as I couldn't find a good simple example – until just now – which is the second example below.

A good "beginner study" script I found checks the available space in all the /var folders and emails the results. This is quite an easy script for none programmers like me to understand and perhaps modify to suit other needs.

The most complex part is understanding the individual character stripping and why, for the awk section. Do your own research on this.

I copied and tested this script and it works fine. I changed the initial 80 limit to 1, so that it will show as low space and so send the email, as I have 80% space left probably in all these folders.

You just have to add your own email address, and have a working MTA like Exim, or command line mailer like mailx or sendEmail or other available, and amend the script to suit them. If you have mailx, great.

https://packages.debian.org/wheezy/bsd-mailx

"mailx is the traditional command-line-mode mail user agent. Even if you don't use it, it may be required by other programs."

From the man mail page:

"Usually, Mail and mailx are just links to mail, which can be confusing."

http://www.linux.com/community/blogs/133-general-linux/748863-linux-shell-script-to-monitor-space-usage-and-send-email

Copy this text below:

---------------------------------------------------------------------------------------------------------------------------------

#!/bin/bash

LIMIT='80'

#Here we declare variable LIMIT with max of used spave

DIR='/var'

#Here we declare variable DIR with name of directory

MAILTO=' you@gmail.com'

#Here we declare variable MAILTO with email address

SUBJECT="$DIR disk usage"

#Here we declare variable SUBJECT with subject of email

MAILX='mailx'

#Here we declare variable MAILX with mailx command that will send email

which $MAILX > /dev/null 2>&1

#Here we check if mailx command exist

if ! [ $? -eq 0 ]

#We check exit status of previous command if exit status not 0 this mean that mailx is not installed on system

then

echo "Please install $MAILX"

#Here we warn user that mailx not installed

exit 1

#Here we will exit from script

fi

cd $DIR

#To check real used size, we need to navigate to folder

USED=`df . | awk '{print $5}' | sed -ne 2p | cut -d"%" -f1`

#This line will get used space of partition where we currently, this will use df command, and get used space in %, and after cut % from value.

if [ $USED -gt $LIMIT ]

#If used space is bigger than LIMIT

then

du -sh ${DIR}/* | $MAILX -s "$SUBJECT" "$MAILTO"

#This will print space usage by each directory inside directory $DIR, and after MAILX will send email with SUBJECT to MAILTO

fi

---------------------------------------------------------------------------------------------------------------------------------

into a file, e.g. varspace.sh:

vi /varspace.sh

make it executable:

chmod +x /varspace.sh

run the script:

sh /varspace.sh

I got this back from my gmail account:

4.0K    /var/backups
659M    /var/cache
348M    /var/lib
4.0K    /var/local
0       /var/lock
25M     /var/log
4.0K    /var/mail
4.0K    /var/opt
0       /var/run
96K     /var/spool
11M     /var/tmp
904M    /var/www

Another handy but simple script which solves the ClamAV issue was found here at step 5:

http://linux.alanstudio.hk/clamav_install.htm

[root@alanstudio]# vi /root/clamscan.sh

/usr/bin/freshclam
rm -f /root/clamscan-report.log
/usr/bin/clamscan -i -r /home /var/www/html --move=/tmp/virusfile/ -l clamscan-report.log
cat clamscan-report.log | mail -s "Clamscan Report" xyz@abc.com

If you don't create the /tmp/virusfile directory, you get an emailed warning:

Clamscan Report

-------------------------------------------------------------------------------

ERROR: '/tmp/virusfile/' doesn't exist or is not a directory

Once that folder is created you get the report for those folders scanned:

Clamscan Report

 

-------------------------------------------------------------------------------

WARNING: /var/www/html: Can't access file

----------- SCAN SUMMARY -----------
Known viruses: 3292516
Engine version: 0.98.1
Scanned directories: 1932
Scanned files: 7855
Infected files: 0
Data scanned: 1147.74 MB
Data read: 1878.36 MB (ratio 0.61:1)
Time: 346.781 sec (5 m 46 s)

Maybe these examples will spur you on to more complex and adventurous things, or lead to learning cmd line scripting tools like awk, and sed for a life as a programmer and a very capable linux admin?

Hmm...not for me.

Comments are closed.

Post Navigation