-
New Feature Request
-
Resolution: Unresolved
-
Major
-
None
-
None
When seeing the function regmatch I thought it was strange it would only return 0 or 1 when it would also be able to return the amount of occurences.
I therefore created the function "regcount" using bash and an entry in zabbix_agentd.conf
http://www.zabbix.com/forum/showthread.php?t=20224
I think it would be useful to many (also other platforms than Linux) to have such a way to count the expressions in a file.
the syntax is:
regcount [-v] <file> <Eregexp>, [ <boolean whole file> ]
It will parse the whole file if the 3rd parameter is 1
If the 3rd parameter is 0 or omitted it will use 'logtail' to check only the difference between the last query
The 3rd parameter can also be higher than 1. In that case it means minutes. This is only useful for Linux logfiles and maybe not such a good idea to implement. It is however quite useful to me.
#!/bin/bash ##################################################### # regcount # returns the occurences of a regular expression in # a file since its last run ##################################################### # Uses logtail & readlink # http://sourceforge.net/projects/logtail/ ##################################################### # echo 'zabbix ALL =(ALL) NOPASSWD: /usr/local/sbin/regcount' >>/etc/sudoers # # grep -iq 'UnsafeUserParameters' /etc/zabbix/zabbix_agentd.conf || sed -i -e 's/^Server.*/&\n\n# Allow regular expressions\nUnsafeUserParameters=1/' /etc/zabbix/zabbix_agentd.conf # # echo 'UserParameter=vfs.file.regcount[*],sudo /usr/local/sbin/regcount "$1" "$2" "$3" "$4" "$5"' >>/etc/zabbix/zabbix_agentd.conf ##################################################### # 08-12-2010 by Frater # # The 3rd parameter (minutes) is optional. # When 0 or empty, it will use 'logtail' which only checks the portion which hasn't been parsed before # When minutes is 1, it will take the whole file # When it's greater than 1, it will use 'lastmins' that will only output the last x minutes ##################################################### # Use at your own risk! ##################################################### export PATH=${PATH}:/usr/local/sbin:/sbin:/usr/sbin:/bin:/usr/bin offset=/tmp/regcount. v= while getopts v name do case $name in v) v='-v ';; ?) printf "Usage: %s: [-v] <file> <Eregexp>, [ <boolean whole file> ]\n" $0 exit 2;; esac done shift $(($OPTIND - 1)) [ ! -h "$1" ] && [ ! -f "$1" ] && exit 1 # [ -z "$2" ] && exit 1 minutes=`echo "$3" | awk -F. '{print $1}' | tr -cd '0-9'` [ -z "${minutes}" ] && minutes=0 if [ -z "$2" ] && [ ${minutes} -eq 1 ] ; then wc -l "$1" | awk '{print $1}' exit 0 fi file2parse="$1" if [ ${minutes} -eq 0 ] ; then fname="`readlink -f "$1"`" fname="`basename "${fname}"`" expression="`echo "$fname.${v}$2$3" | tr -cd '.0-9A-Za-z-'`" offset="${offset}${expression}.offset" ftmp1=`mktemp` logtail -f "$1" -o $offset >${ftmp1} file2parse=${ftmp1} elif [ ${minutes} -gt 1 ] ; then ftmp1=`mktemp` cat "$1" | lastmins ${minutes} >${ftmp1} file2parse=${ftmp1} fi if [ -z "$2" ] ; then wc -l "$file2parse" | awk '{print $1}' else grep $v -cE "$2" "$file2parse" fi rm -f ${ftmp1} 2>/dev/null exit 0