See www.zabbix.com for the official Zabbix site.

User parameter one-liners

From Zabbix.org
Jump to: navigation, search

This site is intended to be a source for or a dump of commands (not scripts) usable as user parameter to extend Zabbix agent functionality.
One-liners normally lack of complex condition evaluation and exception handling but don't need anything else to be deposited.


Note: For better readability command lines might be wrapped. Line breaks are not supported in Zabbix agent configuration and have to be removed.

Contents

Common services

BIND domain name server

Update stats file

Clears stats file and writes current statistics to it.

Note: Statistics file has to exist with proper file permission set
Environment: Linux
Suggested key: custom.bind.updstats
Type of information: str
Example item value: Update succeeded
> /var/named/chroot/var/named/data/named_stats.txt && /usr/bin/sudo /usr/sbin/rndc stats && echo Update succeeded || echo Update failed

Parse stats file

Get specific value from stats file

Note: Relies on a regularly updated stats file
Environment: Linux
Suggested key: custom.bind.stats
Key parameters: metric name
Type of information: uint
Example item key: custom.bind.stats[success]
/bin/awk '$$1=="$1"{print $$2}' /var/named/chroot/var/named/data/named_stats.txt

Parse stats file (bind96)

Get specific value from stats file. Since output may include multiple views the requested value is summed over all views.

Note: Relies on a regularly updated stats file
Environment: Linux
Suggested key: custom.bind96.stats
Key parameters: metric name
Type of information: uint
Example item key: custom.bind96.stats[NXDOMAIN]
/bin/awk '/$1/{s=s+$$1}END{print s}' /var/named/chroot/var/named/data/named_stats.txt

Network Time Protocol daemon

Query NTP daemon

Get value regarding declared system peer

Environment: Linux
Suggested key: custom.ntpq.peer.sys
Key parameters: column number to return
Type of information: int, uint, str
Example item key: custom.ntpq.peer.sys[10]
/usr/sbin/ntpq -pn | /bin/awk '/^\*/{sub("*","",$$1); print $$$1}'

MySQL

Discover all tables in a specific database for an LLD rule:

for table in $(mysql databasename -Ne "show tables;"); do tablelist="$tablelist,"'{"{#TABLE}":"'$table'"}'; done; echo '{"data":['${tablelist#,}']}'

Device-Mapper Multipath

Discover multipath device aliases

Environment: Linux
Suggested key: custom.multipath.alias.discovery
Type of information: text
Example item value: [t|{"data":[{"{#ALIAS}":"mpath0"}]}]

Variation 1

Note: Excessive use might be very resource consuming
/usr/bin/sudo /sbin/multipath -ll -v1 | /bin/awk 'BEGIN{printf "{\"data\":["}{sub(/^[a-z]+:/,"",$1);{printf c"{\"{#ALIAS}\":\""$1"\"}";c=","}}END{printf "]}"}'

Variation 2

Note: Relies on regularly updated cache file
/bin/awk 'BEGIN{printf "{\"data\":["}{sub(/^[a-z]+:/,"",$1);{printf c"{\"{#ALIAS}\":\""$1"\"}";c=","}}END{printf "]}"}' /var/log/zabbix/multipath-ll-v1

Discover paths together with their multipath aliases

Environment: Linux
Suggested key: custom.multipath.path.discovery
Type of information: text
Example item value: [t|{"data":[{"{#ALIAS}":"mpath0","{#PATH}":"sda"}]}]

Variation 1

Note: Excessive use might be very resource consuming
/usr/bin/sudo /sbin/multipath -ll -v2 | \
/bin/awk 'BEGIN{printf "{\"data\":["}{sub(/^[a-z]+:/,"",$1);if($1~/^[[:alnum:]]+$/) m=$1;if(/^ \\_/){printf c"{\"{#ALIAS}\":\""m"\",\"{#PATH}\":\""$3"\"}";c=","}}END{printf "]}"}'

Variation 2

Note: Relies on regularly updated cache file
/bin/awk 'BEGIN{printf "{\"data\":["}{sub(/^[a-z]+:/,"",$1);if($1~/^[[:alnum:]]+$/) m=$1;if(/^ \\_/){printf c"{\"{#ALIAS}\":\""m"\",\"{#PATH}\":\""$3"\"}";c=","}}END{printf "]}"}' \
/var/log/zabbix/multipath-ll-v2

Get specific detail of multipath device

Environment: Linux
Suggested key: custom.multipath.dev.detail
Key parameters: alias, column to return, column content, output column
Type of information: text
Example item key: custom.multipath.dev.detail["mpath0",4,"sda",6]
Example item value: [t|active]

Variation 1

Note: Excessive use might be very resource consuming
/usr/bin/sudo /sbin/multipath -ll -v2 $1 | /bin/awk -F'[ \\])=(\\[]+' '{sub(/^[a-z]+:/,"",$$1);if($$$2=="$3")print $$$4}'

Variation 2

Note: Relies on regularly updated cache file
/bin/awk '$$1~/^[a-z]*:?$1$$/,$$1!~/^[a-z]*:?$1$$/&&$$1~/^[[:alpha:]]/{print $$0}' /var/log/zabbix/multipath-ll-v2 \
| /bin/awk -F'[ \\])=(\\[]+' '{sub(/^[a-z]+:/,"",$$1);if($$$2=="$3")print $$$4}'

Variation 3

Note: Relies on regularly updated cache file
/bin/awk -F'[ \\])=(\\[]+' '$$1~/^[a-z]*:?$1$$/,$$1!~/^[a-z]*:?$1$$/&&$$1~/^[[:alpha:]]/{sub(/^[a-z]+:/,"",$$1);if($$$2=="$3")print $$$4}' /var/log/zabbix/multipath-ll-v2

Get path count of multipath device

Environment: Linux
Suggested key: custom.multipath.dev.pthcnt
Key parameters: alias, path status, device mapper status
Type of information: uint
Example item key: custom.multipath.dev.pthcnt["mpath0","active","ready"]

Variation 1

Note: Excessive use might be very resource consuming
/usr/bin/sudo /sbin/multipath -ll -v2 $1 | grep -c '^ \\_.*$2.*$3'

Variation 2

Note: Relies on regularly updated cache file
/bin/awk '$$1~/^[a-z]*:?$1$$/,$$1!~/^[a-z]*:?$1$$/&&$$1~/^[[:alpha:]]/{print $$0}' /var/log/zabbix/multipath-ll-v2 | grep -c '^ \\_.*$2.*$3'

Variation 3

Note: Relies on regularly updated cache file
/bin/awk -vs=0 '$$1~/^[a-z]*:?$1$$/,$$1!~/^[a-z]*:?$1$$/&&$$1~/^[[:alpha:]]/{if($$0~/^ \\_.*$2.*$3/)s++}END{print s}' /var/log/zabbix/multipath-ll-v2

Update v1 cache file

Environment: Linux
Suggested key: custom.multipath.v1.update
Type of information: uint
/usr/bin/sudo /sbin/multipath -ll -v1 > /var/log/zabbix/multipath-ll-v1.tmp || echo 1 && /bin/mv /var/log/zabbix/multipath-ll-v1.tmp /var/log/zabbix/multipath-ll-v1; echo $?

Update v2 cache file

Environment: Linux
Suggested key: custom.multipath.v2.update
Type of information: uint
/usr/bin/sudo /sbin/multipath -ll -v2 > /var/log/zabbix/multipath-ll-v2.tmp || echo 1 && /bin/mv /var/log/zabbix/multipath-ll-v2.tmp /var/log/zabbix/multipath-ll-v2; echo $?

OpenLDAP directory service

Get lock region statistics

Environment: Linux
Suggested key: custom.slapd.lock
Key parameters: metric name, value column
Type of information: uint
Example item key: custom.slapd.lock["Number of current lockers",1]
/usr/bin/sudo /usr/sbin/slapd_db_stat -h /var/lib/ldap -c \
| /bin/awk '$$2=/$1/{u=$$$2;v=$$$2;gsub("[^0-9]","",v);if(u~"KB"){v=v*1024}else{if(u~"MB"){v=v*1048576}else{if(u~"GB"){v=v*1073741824}else{if(u~"M"){v=v*10000000}}}};print v;exit}'

Get shared memory buffer pool statistics

Environment: Linux
Suggested key: custom.slapd.memory
Key parameters: metric name, value column
Type of information: uint
Example item key: custom.slapd.memory["Pages read into the cache",NF]
/usr/bin/sudo /usr/sbin/slapd_db_stat -h /var/lib/ldap -m \
| /bin/awk '$$2=/$1/{u=$$$2;v=$$$2;gsub("[^0-9]","",v);if(u~"KB"){v=v*1024}else{if(u~"MB"){v=v*1048576}else{if(u~"GB"){v=v*1073741824}else{if(u~"M"){v=v*10000000}}}};print v;exit}'

Get transaction region statistics

Environment: Linux
Suggested key: custom.slapd.txn
Key parameters: metric name, value column
Type of information: uint
Example item key: custom.slapd.txn["Active transactions",1]
/usr/bin/sudo /usr/sbin/slapd_db_stat -h /var/lib/ldap -t \
| /bin/awk '$$2=/$1/{u=$$$2;v=$$$2;gsub("[^0-9]","",v);if(u~"KB"){v=v*1024}else{if(u~"MB"){v=v*1048576}else{if(u~"GB"){v=v*1073741824}else{if(u~"M"){v=v*10000000}}}};print v;exit}'

File parsing

X.509 Certificates from PEM/DER encoded file

Get subject attribute

Get subject attribute from X.509 certificate file

Environment: Linux
Suggested key: custom.x509.file.subject
Key parameters: file format, file, attribute
Type of information: str
Example item key: custom.x509.file.subject[pem,/etc/pki/tls/certs/com.example.www.crt,CN]
Example value: [t|www.example.com]
/usr/bin/openssl x509 -inform '$1' -in '$2' -noout -subject | /bin/sed -n 's%.*$3=\([^/]*\).*%\1%p'

Get issuer attribute

Get issuer attribute from X.509 certificate file

Environment: Linux
Suggested key: custom.x509.file.issuer
Key parameters: file format, file, attribute
Type of information: str
Example item key: custom.x509.file.issuer[pem,/etc/pki/tls/certs/com.example.www.crt,OU]
Example value: [t|Secure Server Certification Authority]
/usr/bin/openssl x509 -inform '$1' -in '$2' -noout -issuer | /bin/sed -n 's%.*$3=\([^/]*\).*%\1%p'

Get validity date

Get start date attribute from X.509 certificate file

Note: Returns 0 when certificate can not be parsed
Environment: Linux
Suggested key: custom.x509.file.startdate
Key parameters: file format, file
Type of information: uint
Example item key: custom.x509.file.startdate[pem,/etc/pki/tls/certs/com.example.www.crt]
D="$(/usr/bin/openssl x509 -inform '$1' -in '$2' -noout -startdate | /bin/cut -d= -f2)"; [[ "$D" ]] && /bin/date -ud "${D##*=}" +%s || echo 0

Get expiration date

Get end date attribute from X.509 certificate file

Note: Returns 0 when certificate can not be parsed
Environment: Linux
Suggested key: custom.x509.file.enddate
Key parameters: file format, file
Type of information: uint
Example item key: custom.x509.file.enddate[pem,/etc/pki/tls/certs/com.example.www.crt]
D="$(/usr/bin/openssl x509 -inform '$1' -in '$2' -noout -enddate | /bin/cut -d= -f2)"; [[ "$D" ]] && /bin/date -ud "${D##*=}" +%s || echo 0

X.509 Certificates from Java keystore

Get subject attribute

Get subject attribute from X.509 certificate which is stored in a Java keystore.

Environment: Linux
Suggested key: custom.x509.jks.subject
Key parameters: keytool path, keystore file, certificate alias, attribute
Type of information: str
Example item key: custom.x509.jks.subject[/usr/java/default/bin,/opt/myapp/conf/keystore.ks,mycert,CN]
Example value: [s|www.example.com]
'$1/keytool' -list -keystore '$2' -alias '$3' -rfc < /dev/null 2> /dev/null | /usr/bin/openssl x509 -noout -subject | /bin/sed -n 's%.*$4=\([^/]*\).*%\1%p'

Get issuer attribute

Get issuer attribute from X.509 certificate which is stored in a Java keystore.

Environment: Linux
Suggested key: custom.x509.jks.issuer
Key parameters: keytool path, keystore file, certificate alias, attribute
Type of information: str
Example item key: custom.x509.jks.issuer[/usr/java/default/bin,/opt/myapp/conf/keystore.ks,mycert,OU]
Example value: [s|Secure Server Certification Authority]
'$1/keytool' -list -keystore '$2' -alias '$3' -rfc < /dev/null 2> /dev/null | /usr/bin/openssl x509 -noout -issuer | /bin/sed -n 's%.*$4=\([^/]*\).*%\1%p'

Get validity date

Get start date attribute from X.509 certificate which is stored in a Java keystore.

Note: Returns 0 when certificate can not be parsed
Environment: Linux
Suggested key: custom.x509.jks.startdate
Key parameters: keytool path, keystore file, certificate alias
Type of information: uint
Example item key: custom.x509.jks.startdate[/usr/java/default/bin,/opt/myapp/conf/keystore.ks,mycert]
D="$('$1/keytool' -list -keystore '$2' -alias '$3' -rfc < /dev/null 2> /dev/null | /usr/bin/openssl x509 -noout -startdate)"; [[ "$D" ]] && /bin/date -ud "${D##*=}" +%s || echo 0

Get expiration date

Get end date attribute from X.509 certificate which is stored in a Java keystore.

Note: Returns 0 when certificate can not be parsed
Environment: Linux
Suggested key: custom.x509.jks.enddate
Key parameters: keytool path, keystore file, certificate alias
Type of information: uint
Example item key: custom.x509.jks.enddate[/usr/java/default/bin,/opt/myapp/conf/keystore.ks,mycert]
D="$('$1/keytool' -list -keystore '$2' -alias '$3' -rfc < /dev/null 2> /dev/null | /usr/bin/openssl x509 -noout -enddate)"; [[ "$D" ]] && /bin/date -ud "${D##*=}" +%s || echo 0

Discover certificates stored in Java keystore

Discover X.509 certificates stored in a Java keystore. Returns keystore file and certificate alias. Up to eight Java keystores can be passed.

Environment: Linux
Suggested key: custom.x509.jks.discovery
Key parameters: keytool path, keystore file
Type of information: text
Example item key: custom.x509.jks.enddate[/usr/java/default/bin,/opt/myapp/conf/keystore.ks]
Example value: [t|{"data":[{"{#FILE}":"/opt/myapp/conf/keystore.ks","{#ALIAS}":"mycert"}]}]
for F in '$2' '$3' '$4' '$5' '$6' '$7' '$8' '$9'; do '$1/keytool' -list -keystore $F < /dev/null 2> /dev/null | /bin/awk -v F=$F -F, '/,/{print F","$$1}'; done \
| /bin/awk -F, 'BEGIN{printf "{\"data\":["};{printf c"{\"{#FILE}\":\""$$1"\",\"{#ALIAS}\":\""$$2"\"}";c=","};END{print "]}"}'

Remote Certificate Checking

X.509 Certificates check using OpenSSL

Get subject attribute

Discover X.509 certificates attribute remotely. Returns subject value.

Environment: Linux
Suggested key: custom.cert.subject
Key parameters: host/server, SSL port
Type of information: str
Example item key: custom.cert.subject[localhost,443]
Example value: [C=US, ST=California, L=Mountain View, O=Big Company, CN=bigcompany.net]
openssl s_client -host $1 -port $2 -showcerts -prexit </dev/null 2>/dev/null |sed -n '/BEGIN CERTIFICATE/,/END CERT/p' | openssl x509 -text 2>/dev/null | sed -n 's/ *Subject: *//p'

Get expiration date attribute

Discover X.509 certificates attribute remotely. Returns expiration date value.

Environment: Linux
Suggested key: custom.cert.expire
Key parameters: host/server, SSL port
Type of information: int
Example item key: custom.cert.expire[localhost,443]
Example value: [May 12 00:00:00 2015 GMT]
openssl s_client -host $1 -port $2 -showcerts -prexit </dev/null 2>/dev/null |sed -n '/BEGIN CERTIFICATE/,/END CERT/p' | openssl x509 -text 2>/dev/null | sed -n 's/ *Not After : *//p'

Virtualization

OpenVZ

Get value from User Beancounters table

Parse User Beancounters table and return a specific value for a resource.
Since /proc/user_beancounters is read in reverse order, this command is sufficient for use on Hardware nodes as well.

Environment: Linux
Suggested key: custom.ovz.ubc
Key parameters: resource name, value column to return
Type of information: uint
Example item key: custom.ovz.ubc[numtcpsock,5]

Variation 1

Note: Excessive use might lead to performance issues, e.g. when using LDAP, because /usr/bin/sudo is called for every measurement
/usr/bin/sudo /usr/bin/tac /proc/user_beancounters | awk '/$1/{print $(NF-5+$2);exit}'

Variation 2

Note: Relies on a regularly updated copy of /proc/user_beancounters
/usr/bin/tac /var/log/zabbix/user_beancounters | awk '/$1/{print $(NF-5+$2);exit}'

Get total fail count from User Beancounters

Parse User Beancounters table and return total count of failed resource allocation from all containers.

Environment: Linux
Suggested key: custom.ovz.ubc.failcnt
Type of information: uint

Variation 1

Note: Excessive use might lead to performance issues, e.g. when using LDAP, because /usr/bin/sudo is called for every measurement
/usr/bin/sudo /usr/bin/tac /proc/user_beancounters | awk '$NF~/^[0-9]+$/{s=s+$NF}END{print s}'

Variation 2

Note: Relies on a regularly updated copy of /proc/user_beancounters
/usr/bin/tac /var/log/zabbix/user_beancounters | awk '$NF~/^[0-9]+$/{s=s+$NF}END{print s}'

Create readable copy of User Beancounters table

Copy User Beancounters table and assure proper file permission.

Environment: Linux
Suggested key: custom.ovz.ubc.update
Type of information: uint
/usr/bin/sudo /usr/bin/install -o zabbix -g zabbix -m 440 /proc/user_beancounters /var/log/zabbix/user_beancounters && echo 0 || echo 1

Get memory utilization in percent of Containers

Get memory information regarding all Containers in percent residing on a Hardware node.

Environment: Linux
Suggested key: custom.ovz.vzmemcheck.putil
Key parameters: column to return
Type of information: int
Example item key: custom.ovz.vzmemcheck.putil[3]
/usr/bin/sudo /usr/sbin/vzmemcheck | awk '/[0-9]/{print $$$1}'

Get memory utilization in Mbytes of Containers

Get memory information regarding all Containers in Mbytes residing on a Hardware node.

Environment: Linux
Suggested key: custom.ovz.vzmemcheck.util
Key parameters: column to return
Type of information: int
Example item key: custom.ovz.vzmemcheck.util[3]
/usr/bin/sudo /usr/sbin/vzmemcheck -A | awk '/[0-9]/{print $$$1;exit}'

Others

DNS lookup

Lookup domain name via dedicated name server.

Environment: Linux
Prerequisites: bind-utils
Suggested key: custom.dig.lookup
Key parameters: server to query, name to look up
Type of information: str
Example item key: custom.dig.lookup[127.0.0.1,example.com]
/usr/bin/dig @$1 $2 A +short +tries=1 +time=1 | /usr/bin/tail -n1

Discover files

Discover files ending with passed string then return file and file name.

Note: If supported, then only readable files are considered
Environment: Linux
Suggested key: custom.discovery.file.end
Key parameters: root path, string files have to end with
Type of information: text
Example item key: custom.discovery.file.end[/etc/pki/tls/certs/,crt]
Example value: [t|{"data":[{"{#FILE}":"/etc/pki/tls/certs/com.example.www.crt","{#FILE_NAME}":"com.example.www.crt"}]}]
(/usr/bin/find '$1' -type f -readable -name '*$2' || /usr/bin/find '$1' -type f -name '*$2') \
| /bin/awk -F/ 'BEGIN{printf "{\"data\":["};{printf c"{\"{#FILE}\":\""$$0"\",\"{#FILE_NAME}\":\""$NF"\"}";c=","};END{print "]}"}'

Discover ActiveMQ queues

Discovery of ActiveMQ queues. Returns broker name and queue name.

Environment: Linux
Suggested key: custom.amq.queue.discovery
Key parameters: URI, user, password
Type of information: text
Example item key: custom.amq.queue.discovery[service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi,zabbix,secret]
Example value: [t|{"data":[{"{#BROKERNAME}":"localhost","{#NAME}":"FOO.QUEUE.BAR"}]}]
/opt/apache-activemq/bin/activemq-admin query\ --jmxurl\ $1\ --jmxuser\ $2\ --jmxpassword\ $3\ -QQueue=\*\ --view\ BrokerName,Name \
| /bin/awk '$$1=="Name"||$$1=="BrokerName"{if(c==",")c="\n";else c=",";printf "\"{#"toupper($$1)"}\":\""$$3"\""c}' \
| /bin/awk 'BEGIN{printf "{\"data\":["};{printf c"{"$$0"}";c=","};END{print "]}"}'

Discover processes with high CPU utilization

Discover processes with a higher than passed threshold of CPU utilization.

Note: Since CPU utilization sample is a very short snapshot, thresholds need to be lower than probably expected.
Environment: AIX
Suggested key: custom.proc.cpu.discovery
Key parameters: threshold
Type of information: text
Example item key: custom.proc.cpu.discovery[0.9]
Example value: [t|{"data":[{"{#USER}":"oraopr","{#COMMAND}":"ora_j000_MYDB"}]}
/usr/bin/ps -e -o "%C;%u;%a" | /usr/bin/awk -F\; '$$1>$1{gsub(/ +$/,"",$$3);print $$2";"$$3;if(++c>2)exit}' | /usr/bin/sort -u | /usr/bin/tr -d '"`*?[]{}~$!&()<>|#@' \
| /usr/bin/awk -F\; 'BEGIN{printf "{\"data\":["}{printf c"{\"{#USER}\":\""$$1"\",\"{#COMMAND}\":\""$$2"\"}";c=","}END{printf "]}"}'

Get the "uptime" of a process

Get the time in seconds since the oldest matching process was created.

Environment: Linux
Suggested key: custom.proc.uptime
Key parameters: user, command sub-string
Type of information: int
Example item key: custom.proc.uptime[prosody,lua]
if [[ -n "$1" ]]; then user="-U $1"; fi;  PID=$(/usr/bin/pgrep -o $$user $2 2>/dev/null); if [[ -z $PID ]]; then echo "ZBX_NOTSUPPORTED";exit 1; fi; echo $(($(/usr/bin/date "+%s") - $(/usr/bin/stat --printf "%Y" /proc/$PID)))

Get CPU utilization for a specific process

Get CPU utilization for a specific process.

Note: Since CPU utilization sample is a very short snapshot, returned value is probably lower than expected.
Environment: AIX
Suggested key: custom.proc.cpu.util
Key parameters: user, command
Type of information: int
Example item key: custom.proc.cpu.util[oraopr,ora_j000_MYDB]
/usr/bin/ps -u '$1' -o args,pcpu | /usr/bin/sort -r | /usr/bin/tr -d '"`*?[]{}~$$!&()<>|#@' | /usr/bin/grep '$2' | /usr/bin/awk '{print $NF;exit}'

Check whether remote host is reachable

Send one ping to passed host and return whether host is reachable (1) or is not reachable (0).

Environment: Linux
Suggested key: custom.ping.reachable
Key parameters: remote host to check
Type of information: uint
Example item key: custom.ping.reachable[example.com]

Variation 1

if [[ $(/bin/ping -c 1 -W 1 -q -n $1 2> /dev/null | /bin/grep received | /bin/awk -F ',' '{ print $$2 }' | awk '{ print $$1 }') ]]; then echo 1; else echo 0; fi

Variation 2

/bin/ping -c 1 -W 1 -q -n $1 &> /dev/null && echo 1 || echo 0

Discover block devices

Discover block devices then return device names.

Environment: Linux
Suggested key: custom.discovery.partitions
Type of information: text
Example value: [t|{"data":[{"{#DEV}":"sda"}]}]
/bin/awk 'BEGIN{printf "{\"data\":["};$1~/[0-9]/{printf c"{\"{#DEV}\":\""$NF"\"}";c=","};END{print "]}"}' /proc/partitions

Using lsblk and LLD is a more complex solution.