root / plugins / znc / znc_logs.py @ 9bc39fb2
Historique | Voir | Annoter | Télécharger (2,62 ko)
| 1 | 8fe960de | Thor77 | #!/usr/bin/python3
|
|---|---|---|---|
| 2 | # -*- coding: utf-8 -*-
|
||
| 3 | '''
|
||
| 4 | =head1 NAME
|
||
| 5 | znc_logs
|
||
| 6 |
|
||
| 7 | =head1 DESCRIPTION
|
||
| 8 | Shows lines/minute in today's znc-logs
|
||
| 9 |
|
||
| 10 | =head2 CONFIGURATION
|
||
| 11 | [znc_logs]
|
||
| 12 | user znc # or any other user/group that can read the znclog-folder
|
||
| 13 | group znc
|
||
| 14 | env.logdir /var/lib/znc/moddata/log/ # path to the log-folder with a "/" at the end
|
||
| 15 |
|
||
| 16 | =head1 COPYRIGHT
|
||
| 17 | GPL VERSION 3
|
||
| 18 |
|
||
| 19 | =head1 AUTHOR
|
||
| 20 | Thor77 <thor77[at]thor77.org>
|
||
| 21 | '''
|
||
| 22 | from sys import argv |
||
| 23 | from time import strftime |
||
| 24 | from os import environ, listdir |
||
| 25 | |||
| 26 | logdir = environ.get('logdir')
|
||
| 27 | |||
| 28 | if not logdir: |
||
| 29 | raise Exception('You have to set the logdir with env.logdir <path to log> in the plugin-conf!') |
||
| 30 | |||
| 31 | date = strftime('%Y%m%d')
|
||
| 32 | last_values_file = environ['MUNIN_PLUGSTATE'] + '/last_values' |
||
| 33 | |||
| 34 | |||
| 35 | def get_last(): |
||
| 36 | try:
|
||
| 37 | d = {}
|
||
| 38 | with open(last_values_file, 'r') as f: |
||
| 39 | for line in f: |
||
| 40 | line = line[:-1]
|
||
| 41 | key, value = line.split(':')
|
||
| 42 | d[key] = float(value)
|
||
| 43 | return d
|
||
| 44 | except FileNotFoundError:
|
||
| 45 | return {}
|
||
| 46 | |||
| 47 | |||
| 48 | def data(): |
||
| 49 | last = get_last() |
||
| 50 | current = {}
|
||
| 51 | for filename in listdir(logdir): |
||
| 52 | filename_ = filename.replace('.log', '') |
||
| 53 | network, channel, file_date = filename_.split('_')
|
||
| 54 | network_channel = '{}_{}'.format(network, channel)
|
||
| 55 | # check if log is from today and it is a channel
|
||
| 56 | if file_date == date and channel.startswith('#'): |
||
| 57 | # current lines in the file
|
||
| 58 | current_value = sum(1 for i in open(logdir + filename, 'r', encoding='utf-8', errors='replace')) |
||
| 59 | |||
| 60 | if network_channel not in last: |
||
| 61 | value = 0
|
||
| 62 | else:
|
||
| 63 | last_value = last[network_channel] |
||
| 64 | # what munin gets
|
||
| 65 | value = (current_value - last_value) / 5 # subtrate last from current and divide through 5 to get new lines / minute |
||
| 66 | 9bc39fb2 | Thor77 | if value < 0: |
| 67 | value = 0
|
||
| 68 | 8fe960de | Thor77 | # save it to the states-file
|
| 69 | current[network_channel] = current_value |
||
| 70 | |||
| 71 | # print things to munin
|
||
| 72 | network_channel = network_channel.replace('.', '').replace('#', '') |
||
| 73 | print('{network_channel}.label {channel}@{network}'.format(network_channel=network_channel, channel=channel, network=network))
|
||
| 74 | print('{network_channel}.value {value}'.format(network_channel=network_channel, value=value))
|
||
| 75 | with open(last_values_file, 'w') as f: |
||
| 76 | for k in current: |
||
| 77 | f.write('{}:{}\n'.format(k, current[k]))
|
||
| 78 | |||
| 79 | |||
| 80 | if len(argv) > 1 and argv[1] == 'config': |
||
| 81 | print('graph_title Lines in the ZNC-log')
|
||
| 82 | print('graph_category znc')
|
||
| 83 | print('graph_vlabel lines/minute')
|
||
| 84 | print('graph_scale no')
|
||
| 85 | data() |
