diff --git a/plugins/znc/README.md b/plugins/znc/README.md new file mode 100644 index 00000000..0f7204fc --- /dev/null +++ b/plugins/znc/README.md @@ -0,0 +1,36 @@ +ZNC-Logs +======== + +# Configuration +1. Make sure Python3 is installed and added to your path, this plugin will try to use `/usr/bin/python3` + +2. Enable the log-plugin in znc (if you enable it for the complete instance, there will be some issues because this plugin only uses the network-name + channel-name, if there are some networks with the same name, it will count all lines together) + +3. Add this to your `/etc/munin/plugin-conf.d/munin-node` +``` +[znc_logs] +user $your_znc_user +group $your_znc_group +env.logdir $path_to_znc_logs +``` +`$your_znc_user` has to be an user, which can list `$path_to_znc_logs` and read all files in this directory. + +4. Run `sudo wget -O /usr/share/munin/plugins/znc_logs https://raw.githubusercontent.com/munin-monitoring/contrib/master/plugins/znc/znc_logs.py` to download the `znc_logs.py` file from github to `/usr/share/munin/plugins/znc_logs` (of course you can use any directory you want) + +5. Make the plugin-file executable `sudo chmod +x /usr/share/munin/plugins/znc_logs` + +6. Enable the plugin with `sudo ln -s /usr/share/munin/plugins/znc_logs /etc/munin/plugins/` + +7. Try the plugin with `sudo munin-run znc_logs` + +7. Restart munin-node to load the plugin: `sudo service munin-node restart` + +# FAQ +## What is ZNC? +ZNC is an IRC-Bouncer (a proxy to irc), further information: [znc.in](http://wiki.znc.in/ZNC) + +## What does this plugin do? +This plugin counts the lines/minute in znc's log-files + +# Contact +If you experience any issues, feel free to contact me at thor77[at]thor77.org \ No newline at end of file diff --git a/plugins/znc/example-graphs/znc_logs-day.png b/plugins/znc/example-graphs/znc_logs-day.png new file mode 100644 index 00000000..fd65c8c3 Binary files /dev/null and b/plugins/znc/example-graphs/znc_logs-day.png differ diff --git a/plugins/znc/znc_logs.py b/plugins/znc/znc_logs.py new file mode 100644 index 00000000..26eee0cc --- /dev/null +++ b/plugins/znc/znc_logs.py @@ -0,0 +1,85 @@ +#!/usr/bin/python3 +# -*- coding: utf-8 -*- +''' +=head1 NAME +znc_logs + +=head1 DESCRIPTION +Shows lines/minute in today's znc-logs + +=head2 CONFIGURATION +[znc_logs] +user znc # or any other user/group that can read the znclog-folder +group znc +env.logdir /var/lib/znc/moddata/log/ # path to the log-folder with a "/" at the end + +=head1 COPYRIGHT +GPL VERSION 3 + +=head1 AUTHOR +Thor77 +''' +from sys import argv +from time import strftime +from os import environ, listdir + +logdir = environ.get('logdir') + +if not logdir: + raise Exception('You have to set the logdir with env.logdir in the plugin-conf!') + +date = strftime('%Y%m%d') +last_values_file = environ['MUNIN_PLUGSTATE'] + '/last_values' + + +def get_last(): + try: + d = {} + with open(last_values_file, 'r') as f: + for line in f: + line = line[:-1] + key, value = line.split(':') + d[key] = float(value) + return d + except FileNotFoundError: + return {} + + +def data(): + last = get_last() + current = {} + for filename in listdir(logdir): + filename_ = filename.replace('.log', '') + network, channel, file_date = filename_.split('_') + network_channel = '{}_{}'.format(network, channel) + # check if log is from today and it is a channel + if file_date == date and channel.startswith('#'): + # current lines in the file + current_value = sum(1 for i in open(logdir + filename, 'r', encoding='utf-8', errors='replace')) + + if network_channel not in last: + value = 0 + else: + last_value = last[network_channel] + # what munin gets + value = (current_value - last_value) / 5 # subtrate last from current and divide through 5 to get new lines / minute + if value < 0: + value = 0 + # save it to the states-file + current[network_channel] = current_value + + # print things to munin + network_channel = network_channel.replace('.', '').replace('#', '') + print('{network_channel}.label {channel}@{network}'.format(network_channel=network_channel, channel=channel, network=network)) + print('{network_channel}.value {value}'.format(network_channel=network_channel, value=value)) + with open(last_values_file, 'w') as f: + for k in current: + f.write('{}:{}\n'.format(k, current[k])) + + +if len(argv) > 1 and argv[1] == 'config': + print('graph_title Lines in the ZNC-log') + print('graph_category znc') + print('graph_vlabel lines/minute') + print('graph_scale no') +data()