Koozali.org: home of the SME Server

bash script check Clamav virus database version of the service to the website

Offline purvis

  • *****
  • 567
  • +0/-0
A bash script that compares the virus database of the running clamav daemon to what is listed on the website of Clamav.
This script can help decide whether the cvd files need to be updated or if freshclam might not be doing its job.
Clamd service has to be running and access to the internet and to the Clamav website has to be operational.
This is the bare minimum of code needed.

Extra code to add:
Check for an active clamd service running will be added later. I can do that.
Code to check the daily.cvd or daily.cld for the version numbers. I can do that.
Code to check for an internet connection. Then that would be a plus to add into this script too.

This code is designed to return a value that indicates the
Code: [Select]
#!/bin/bash

#clamcurrentck is bash script name
#THIS BASH ROUTINE WILL CHECK TO SEE IF THE CURRENT CLAMAV VIRUS
#DATABASE IS THE THE MOST CURRENT FROM THE INTERNET SIT

###############################  VARIABLES THAT NEED SETTING
#  COMMAND TO PROVIDE VERSION OF CLAMAV SERVICE
#  THIS VARIABLE IS SET TO "clamd -V" by default
   commandtocheckclamavservice="clamd -V"
#  COMMAND TO PROVIDE THE CURRENT VERSION FROM CLAMAV INTERNET SITE
   commandtocheckclamavsite="host -t txt current.cvd.clamav.net"

############################### END OF VARIABLE

function parseiteminstr () {
local i=0
local x=0
local arr2=""
local itemnumber=""
parseiteminstring=""
if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ];then return;fi
OIFS=$IFS;IFS="$2"
i=0
itemnumber="$3"
arr2="$1"
for x in $arr2
do
let i+=1
if [ $i == $itemnumber ];then parseiteminstring=$x;fi
done
IFS=$OIFS
}

#  FUNCTION TO RETURN A VALUE OF ZERO OR ONE IF THE CLAMAV DB IS CURRENT
#  0 IS NOT CURRENT AND 1 IS CURRENT
function isclamdbversioncurrent () {
local tempstring=""
local clamdbversionfromservice=""
local clamdbversionfromsite=""
clamdbversionuptodate=0
parseiteminstring=""
tempstring=$($commandtocheckclamavservice)
parseiteminstr "$tempstring" "/" "2"
clamdbversionfromservice=$parseiteminstring
#THE NEXT 4 LINES ARE FOR TESTING THE RETURN VARIABLE
#print length of variable
#echo $parseiteminstring
#print the parseiteminstring variable length
#echo $(expr length "$parseiteminstring")

clamdbversionservice=$parseiteminstring
parseiteminstring=""
tempstring=""
tempstring=$(echo $($commandtocheckclamavsite) | tr -d '"')
parseiteminstr "$tempstring"  ":" "3"
clamdbversionfromsite=$parseiteminstring

if [ -z "$clamdbversionfromsite" ];then  let clamdbversionuptodate-=2;fi
if [ -z "$clamdbversionfromservice" ];then let clamdbversionuptodate-=1;fi
if [ $clamdbversionuptodate -ne 0 ];then return 0;fi
if [ "$clamdbversionfromservice" == "$clamdbversionfromsite" ];then let clamdbversionuptodate=1;fi
if [ $clamdbversionuptodate -eq 1 ] || [ $clamdbversionuptodate -eq 0 ];then
   echo "Clamav virus database from Clamav service is $clamdbversionfromservice"
   echo "Clamav virus database from Clamav website is $clamdbversionfromsite"
fi

}
clamdbversionuptodate=0
isclamdbversioncurrent
case "$clamdbversionuptodate" in
 1)  echo "Clamav virus database is up to date "
    ;;
 0)  echo "ClamAV virus database is NOT up to date"
    ;;
-1) echo "Error cannot get ClamAV db version from local command"
    ;;
-2) echo "Error cannot get ClamAV db version from internet"
    ;;
-3) echo "Error cannot get ClamAV db version from both command and internet"
    ;;
 *) echo "Error returned value of $clamdbversionuptodate is not defined"
    ;;
esac
« Last Edit: March 22, 2013, 01:10:10 PM by purvis »