Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / other / i2p_ @ 8a482a95

Historique | Voir | Annoter | Télécharger (2,44 ko)

1
#!/usr/bin/env python
2

    
3
# Version 0.1 alpha (a.k.a. it has been known to work at least once)
4
# Get stats from your i2p server ( https://geti2p.net/en/ )
5
# Create links to this plugin and name them
6
# - i2p_bps
7
# - i2p_uptime (not implemented yet)
8
# Requires BeautifulSoup 4
9

    
10
# Should probably use I2PControl for this instead
11
# https://geti2p.net/en/docs/api/i2pcontrol
12

    
13
import urllib2, re, os, sys
14
from bs4 import BeautifulSoup
15
from decimal import *
16

    
17
plugin_name=list(os.path.split(sys.argv[0]))[1]
18
plugin_var=plugin_name.split('_', 1)[-1]
19

    
20
def autoconf():
21
  print('yes')
22
  sys.exit(0)
23

    
24
def config():
25
  if 'bps' == plugin_var:
26
    print('graph_title i2p bps')
27
    print('graph_vlabel bps')
28
    print('graph_info i2p sending and receiving bytes per second')
29
    print('graph_category i2p')
30
    print('receivebps.label Receive bps')
31
    print('sendbps.label Send bps')
32
  elif 'uptime' == plugin_var:
33
    print('graph_title i2p uptime')
34
    print('graph_scale no')
35
    print('graph_args --base 1000 -l 0')
36
    print('graph_vlabel uptime in whole hours')
37
    print('graph_category i2p')
38
    print('uptime.label i2p uptime')
39
    print('uptime.draw AREA')
40
  else:
41
    raise ValueError, "unknown parameter '%s'" % plugin_var
42
  sys.exit(0)
43

    
44
def fetch():
45
  html_doc = urllib2.urlopen('http://127.0.0.1:7657/stats').read()
46
  soup = BeautifulSoup(html_doc)
47
  if 'bps' == plugin_var:
48
    fetch_bps(soup)
49
  elif 'uptime' == plugin_var:
50
    fetch_uptime(soup)
51
  else:
52
    raise ValueError, "unknown parameter '%s'" % plugin_var
53

    
54
def fetch_bps(soup):
55
  anchor_bwreceiveBps = soup.find('a', attrs={"name": "bw.receiveBps"})
56
  b_5min_bwreceiveBps = anchor_bwreceiveBps.find_all_next('b', limit=2)[1]
57
  bwreceiveBps = Decimal(re.search('Average: ([0-9,\.]+?);', b_5min_bwreceiveBps.parent.get_text()).group(1).replace(',', ''))
58
  anchor_bwsendBps = soup.find('a', attrs={"name": "bw.sendBps"})
59
  b_5min_bwsendBps = anchor_bwsendBps.find_all_next('b', limit=2)[1]
60
  bwsendBps = Decimal(re.search('Average: ([0-9,\.]+?);', b_5min_bwsendBps.parent.get_text()).group(1).replace(',', ''))
61
  print('receivebps.value %s' % bwreceiveBps)
62
  print('sendbps.value %s' % bwsendBps)
63
  sys.exit(0)
64

    
65
def fetch_uptime(soup):
66
  # not implemented yet
67
  print('uptime.value U')
68
  sys.exit(0)
69

    
70
if __name__ == '__main__':
71
  if len(sys.argv)>1 :
72
    if sys.argv[1]=="config" :
73
      config()
74
    elif sys.argv[1]=="autoconf" :
75
      autoconf()
76
    elif sys.argv[1]!="":
77
      raise ValueError, "unknown parameter '%s'" % sys.argv[1]
78
  fetch()