aboutsummaryrefslogtreecommitdiffstats
path: root/xpybar/config/mystat.py
blob: d358d15101b2404ca918f718f67e9e38ccba32b2 (plain) (blame)
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
80
81
82
83
84
85
86
87
88
89
90
91
# -*- python -*-
from plugins.snmp   import SNMP
from plugins.snmp6  import SNMP6
from plugins.uptime import Uptime
from plugins.vmstat import VMStat

from common import *

class MyStat(Entry):
    def __init__(self, *args, **kwargs):
        self.keys_4 = SNMP().keys
        self.keys_6 = SNMP6().keys
        self.keys_vm = VMStat().keys
        self.show_group = 0
        self.show_value_4 = 0
        self.show_value_6 = 0
        self.show_value_vm = 0
        Entry.__init__(self, *args, **kwargs)
    
    def action(self, col, button, x, y):
        if button == LEFT_BUTTON:
            n = self.show_group + 1
            if n < 4:
                self.show_group = n
                self.invalidate()
        elif button == MIDDLE_BUTTON:
            self.invalidate()
        elif button == RIGHT_BUTTON:
            n = self.show_group
            if n > 0:
                self.show_group = n - 1
                self.invalidate()
        elif button == SCROLL_UP:
            if self.show_group == 0:
                n = self.show_value_4
                if n + 1 < len(self.keys_4):
                    self.show_value_4 = n + 1
                    self.invalidate()
            elif self.show_group == 1:
                n = self.show_value_6
                if n + 1 < len(self.keys_6):
                    self.show_value_6 = n + 1
                    self.invalidate()
            elif self.show_group == 2:
                n = self.show_value_vm
                if n + 1 < len(self.keys_vm):
                    self.show_value_vm = n + 1
                    self.invalidate()
            elif self.show_group == 3:
                pass
        elif button == SCROLL_DOWN:
            if self.show_group == 0:
                n = self.show_value_4
                if n > 0:
                    self.show_value_4 = n - 1
                    self.invalidate()
            elif self.show_group == 1:
                n = self.show_value_6
                if n > 0:
                    self.show_value_6 = n - 1
                    self.invalidate()
            elif self.show_group == 2:
                n = self.show_value_vm
                if n > 0:
                    self.show_value_vm = n - 1
                    self.invalidate()
            elif self.show_group == 3:
                pass
    
    def function(self):
        try:
            if self.show_group == 0:
                label = self.keys_4[self.show_value_4]
                value = SNMP()[label]
            elif self.show_group == 1:
                label = self.keys_6[self.show_value_6]
                value = SNMP6()[label]
            elif self.show_group == 2:
                label = self.keys_vm[self.show_value_vm]
                value = VMStat()[label]
                label = ''.join(w[:1].upper() + w[1:] for w in label.split('_'))
            elif self.show_group == 3:
                uptime = Uptime()
                avg = uptime.uptime_seconds - uptime.average_idle_seconds
                wrk = (avg) * 100 / uptime.uptime_seconds
                up  = '%id%i:%02i' % uptime.uptime[:3]
                avg = '%id%i:%02i' % Uptime.split_time(avg)[:3]
                return 'Uptime: %.0f%% %s / %s' % (wrk, avg, up)
            return '%s: %i' % (label, value)
        except Exception as e:
            return str(e)