summaryrefslogtreecommitdiffstats
path: root/collectors/python.d.plugin/logind/logind.chart.py
blob: bfc486c7ff998902b281ce1baf304a539f6114e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# -*- coding: utf-8 -*-
# Description: logind netdata python.d module
# Author: Austin S. Hemmelgarn (Ferroin)
# SPDX-License-Identifier: GPL-3.0-or-later

from bases.FrameworkServices.ExecutableService import ExecutableService

priority = 59999
disabled_by_default = True

ORDER = ['sessions', 'users', 'seats']

CHARTS = {
    'sessions': {
        'options': [None, 'Logind Sessions', 'sessions', 'sessions', 'logind.sessions', 'stacked'],
        'lines': [
            ['sessions_graphical', 'Graphical', 'absolute', 1, 1],
            ['sessions_console', 'Console', 'absolute', 1, 1],
            ['sessions_remote', 'Remote', 'absolute', 1, 1]
        ]
    },
    'users': {
        'options': [None, 'Logind Users', 'users', 'users', 'logind.users', 'stacked'],
        'lines': [
            ['users_graphical', 'Graphical', 'absolute', 1, 1],
            ['users_console', 'Console', 'absolute', 1, 1],
            ['users_remote', 'Remote', 'absolute', 1, 1]
        ]
    },
    'seats': {
        'options': [None, 'Logind Seats', 'seats', 'seats', 'logind.seats', 'line'],
        'lines': [
            ['seats', 'Active Seats', 'absolute', 1, 1]
        ]
    }
}


class Service(ExecutableService):
    def __init__(self, configuration=None, name=None):
        ExecutableService.__init__(self, configuration=configuration, name=name)
        self.command = 'loginctl list-sessions --no-legend'
        self.order = ORDER
        self.definitions = CHARTS

    def _get_data(self):
        ret = {
            'sessions_graphical': 0,
            'sessions_console': 0,
            'sessions_remote': 0,
        }
        users = {
            'graphical': list(),
            'console': list(),
            'remote': list()
        }
        seats = list()
        data = self._get_raw_data()

        for item in data:
            fields = item.split()
            if len(fields) == 3:
                users['remote'].append(fields[2])
                ret['sessions_remote'] += 1
            elif len(fields) == 4:
                users['graphical'].append(fields[2])
                ret['sessions_graphical'] += 1
                seats.append(fields[3])
            elif len(fields) == 5:
                users['console'].append(fields[2])
                ret['sessions_console'] += 1
                seats.append(fields[3])

        ret['users_graphical'] = len(set(users['graphical']))
        ret['users_console'] = len(set(users['console']))
        ret['users_remote'] = len(set(users['remote']))
        ret['seats'] = len(set(seats))

        return ret