forked from hyphanet/pyFreenet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fcp_to_rrdtool_bridge.py
executable file
·119 lines (108 loc) · 2.94 KB
/
fcp_to_rrdtool_bridge.py
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/env python
"""
Output specified node stats in a format expected by rrdtool
This code was written by Zothar, April 2007, released under the GNU Lesser General
Public License.
No warranty, yada yada
Example usage: rrdtool update freenet.rrd `/usr/local/pyFreenet/fcp_to_rrdtool_bridge.py averagePingTime,RunningThreadCount,location,locationChangePerSession`
"""
import fcp3 as fcp;
import sys;
stat_fields = [];
host = "127.0.0.1";
port = 9481;
list_fields_flag = False;
def usage():
print("Usage: %s [<host>[:<port>]] [<stat_field>[,<stat_field> ...]]" % ( sys.argv[ 0 ] ));
print(" %s [<host>[:<port>]] --list-fields" % ( sys.argv[ 0 ] ));
print();
print("Example:");
print(" %s averagePingTime,runningThreadCount,location,locationChangePerSession" % ( sys.argv[ 0 ] ));
argv = sys.argv;
arg0 = argv[ 0 ];
argv = argv[ 1: ];
argc = len( argv );
if( 0 == argc ):
usage();
sys.exit( 1 );
for arg in argv:
if( "--" == arg[ :2 ] ):
option = arg[ 2: ];
if( "list-fields" == option ):
list_fields_flag = True;
else:
print("Unknown option: %s" % ( arg ));
print();
usage();
sys.exit( 1 );
i = arg.find( ":" );
if( -1 != i ):
argfields = arg.split( ":" );
if( 2 != len( argfields )):
usage();
sys.exit( 1 );
host = argfields[ 0 ];
port = int( argfields[ 1 ] );
continue;
i = arg.find( "," );
if( -1 != i ):
argfields = arg.split( "," );
for argfield in argfields:
if( 0 == len( argfield )):
continue;
stat_fields.append( argfield );
continue;
i = arg.find( "." );
if( -1 != i ):
host = arg;
continue;
stat_fields.append( arg );
if( 0 == len( stat_fields ) and not list_fields_flag ):
print("Must specify stat_fields when not using --list-fields");
print();
usage();
sys.exit( 1 );
f = fcp.FCPNode( host = host, port = port );
entry = f.refstats( WithVolatile = True );
f.shutdown();
if( list_fields_flag ):
keys = list(entry.keys());
keys.sort();
print("Volatile fields:");
for key in keys:
if( not key.startswith( "volatile." )):
continue;
print(key[ 9: ]);
print();
print("non-volatile fields:")
for key in keys:
if( key.startswith( "volatile." )):
continue;
print(key);
else:
field_count = 0;
sys.stdout.write( "N" );
for stat_field in stat_fields:
try:
datum_string = entry[ "volatile." + stat_field ];
except KeyError as msg:
try:
datum_string = entry[ stat_field ];
except KeyError as msg:
datum_string = "0";
field_count += 1;
if( "true" == datum_string ):
datum = 1.0;
elif( "false" == datum_string ):
datum = 0.0;
else:
datum = float( datum_string );
try:
test_datum = int( datum );
except ValueError as msg:
test_datum = None;
if( test_datum == datum ):
datum = test_datum;
sys.stdout.write( ":%s" % ( datum ));
sys.stdout.write( "\n" );
sys.stdout.flush();