Skip to content

Instantly share code, notes, and snippets.

@kamermans
Last active August 29, 2015 14:18
Show Gist options
  • Save kamermans/ca5f92f6bec87c3f7470 to your computer and use it in GitHub Desktop.
Save kamermans/ca5f92f6bec87c3f7470 to your computer and use it in GitHub Desktop.
Rsyslog impstats data parser for Zabbix (or anything else really)
#!/usr/bin/env perl
# Rsyslog Stats parser by Steve Kamerman
# To use:
#
# 1. Enable imstats in /etc/rsyslog.conf by putting this at the top:
#
# module(load="impstats"
# interval="300"
# severity="7"
# log.syslog="off"
# log.file="/var/log/rsyslog-stats.log")
#
# 2. Create the log file like this:
#
# RLOG=/var/log/rsyslog-stats.log && touch $RLOG && chown syslog:syslog $RLOG
#
# 3. Restart rsyslogd
#
# service rsyslogd restart
#
# 4. Enable remote commands in /etc/zabbix/zabbix_agentd.conf:
#
# EnableRemoteCommands=1
#
# 5. Restart zabbix agent
#
# service zabbix-agent restart
#
# 6. Define items with keys like this:
#
# system.run["/opt/getRsyslogMetric.pl core.queue::enqueued"]
#
# The entire list of possible keys (like core.queue::enqueued above) can be
# shown with live values by running /opt/getRsyslogMetric.pl without arguments.
use strict;
my $logfile = "/var/log/rsyslog-stats.log";
# Mon Mar 30 19:57:37 2015: resource-usage: origin=impstats utime=10786295 stime=6334465 maxrss=4588 minflt=692 majflt=0 inblock=0 oublock=528560 nvcsw=476509 nivcsw=694
# Mon Mar 30 19:57:37 2015: main Q: origin=core.queue size=0 enqueued=393976 full=0 discarded.full=0 discarded.nf=0 maxqsize=19
my $get_key = shift || 0;
my @lines = `tail -n50 $logfile | grep -E "(resource-usage|forwardRelp:|main Q)" | tail -n2`;
foreach (@lines) {
my $origin;
my @parts = split(/ /, $_);
foreach (@parts) {
if (m/^(.+?)=(.+)/) {
if ($1 eq "origin") {
$origin = $2;
next;
}
if ($get_key eq 0) {
print "$origin::$1=$2\n";
} else {
if ($get_key eq "$origin::$1") {
print "$2\n";
exit;
}
}
}
}
}
if ($get_key ne 0) {
die("Error: Key $get_key does not exist\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment