Projet

Général

Profil

Révision 8fe960de

ID8fe960de7bd084311ebcec09155f303f9336b382
Parent 341798cd
Enfant 9bc39fb2

Ajouté par Thor77 il y a presque 11 ans

add znc_logs plugin

Voir les différences:

plugins/znc/znc_logs.py
1
#!/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

  
67
            # save it to the states-file
68
            current[network_channel] = current_value
69

  
70
            # print things to munin
71
            network_channel = network_channel.replace('.', '').replace('#', '')
72
            print('{network_channel}.label {channel}@{network}'.format(network_channel=network_channel, channel=channel, network=network))
73
            print('{network_channel}.value {value}'.format(network_channel=network_channel, value=value))
74
    with open(last_values_file, 'w') as f:
75
        for k in current:
76
            f.write('{}:{}\n'.format(k, current[k]))
77

  
78

  
79
if len(argv) > 1 and argv[1] == 'config':
80
    print('graph_title Lines in the ZNC-log')
81
    print('graph_category znc')
82
    print('graph_vlabel lines/minute')
83
    print('graph_scale no')
84
data()

Formats disponibles : Unified diff