Преглед изворни кода

a functional ping plugin, but needs moar exceptions and sanity checks

master
Von Random пре 2 година
родитељ
комит
d77dbeb1de
5 измењених фајлова са 76 додато и 6 уклоњено
  1. 12
    2
      conf.ini
  2. 1
    1
      plugins/date.py
  3. 49
    0
      plugins/ping.py
  4. 2
    2
      vdstatus.py
  5. 12
    1
      vdstatus_i3

+ 12
- 2
conf.ini Прегледај датотеку

@@ -1,10 +1,20 @@
1 1
 [main]
2 2
 
3
+[ping_ext]
4
+plugin = ping
5
+hosts = de-ber-as20647.anchors.atlas.ripe.net,nl-ams-as1101.anchors.atlas.ripe.net,uk-boh-as196745.anchors.atlas.ripe.net
6
+title = NET:
7
+
8
+[ping_local]
9
+plugin = ping
10
+hosts = 127.0.0.1,127.0.0.2
11
+title = LOCAL:
12
+
3 13
 [day]
4 14
 plugin = date
5
-color = red
15
+color = #FF0000
6 16
 format = %%A %%d
7 17
 
8 18
 [time]
9 19
 plugin = date
10
-format = %%H:%%M
20
+format = %%H:%%M

+ 1
- 1
plugins/date.py Прегледај датотеку

@@ -1,7 +1,7 @@
1 1
 import time
2 2
 
3 3
 
4
-def run(config, section):
4
+def execute(config, section):
5 5
     fmt = config.get(section, 'format')
6 6
     result = dict()
7 7
     if config.has_option(section, 'color'):

+ 49
- 0
plugins/ping.py Прегледај датотеку

@@ -0,0 +1,49 @@
1
+import os
2
+import random
3
+import time
4
+import threading
5
+
6
+
7
+class ConnTest(threading.Thread):
8
+    def __init__(self, threadID, name):
9
+        threading.Thread.__init__(self)
10
+        self.threadID = threadID
11
+        self.name = name
12
+        self.hosts = list()
13
+        self.status = 'n/a'
14
+        self.freq = int()
15
+
16
+    def configure(self, hosts, freq=5):
17
+        self.hosts = hosts.split(',')
18
+        self.freq = freq
19
+
20
+    def run(self):
21
+        global is_running
22
+        is_running = True
23
+        while True:
24
+            random.shuffle(self.hosts)
25
+            try:
26
+                for host in self.hosts:
27
+                    fping = 'fping -q -c1 -t100 ' + host + ' &>/dev/null'
28
+                    response = os.system(fping)
29
+                    if response == 0:
30
+                        self.status = 'on'
31
+                        break
32
+                    self.status = 'off'
33
+
34
+            except (KeyboardInterrupt, SystemExit):
35
+                break
36
+            time.sleep(self.freq)
37
+
38
+is_running = False
39
+ping_object = ConnTest(1, 'test_network')
40
+
41
+
42
+def execute(config, section):
43
+    result = dict()
44
+    global ping_object
45
+    if is_running is False:
46
+        ping_object.configure(config.get(section, 'hosts'), 10)
47
+        ping_object.start()
48
+    result['full_text'] = config.get(section, 'title') + ' ' + ping_object.status
49
+    return result

+ 2
- 2
vdstatus.py Прегледај датотеку

@@ -18,5 +18,5 @@ def run_plugins():
18 18
             continue
19 19
         plugin_name = '.' + configuration.get(section, 'plugin')
20 20
         plugin_module = importlib.import_module(plugin_name, 'plugins')
21
-        outputs.append(plugin_module.run(configuration, section))
22
-    print(json.dumps(outputs) + ',')
21
+        outputs.append(plugin_module.execute(configuration, section))
22
+    return outputs

+ 12
- 1
vdstatus_i3 Прегледај датотеку

@@ -1 +1,12 @@
1
-#!/usr/bin/env python3 -u
1
+#!/usr/bin/python3 -u
2
+import json
3
+import vdstatus
4
+import time
5
+
6
+
7
+print('{"version":1}')
8
+print('[')
9
+while True:
10
+    output = vdstatus.run_plugins()
11
+    print(json.dumps(output) + ',')
12
+    time.sleep(1)

Loading…
Откажи
Сачувај