File: gr_rup

package info (click to toggle)
grmonitor 0.81-4.2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,556 kB
  • ctags: 1,767
  • sloc: ansic: 12,657; python: 186; makefile: 175
file content (97 lines) | stat: -rwxr-xr-x 2,391 bytes parent folder | download | duplicates (5)
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
#!/usr/bin/python
import string, os, time, sys

#
# Gather rup info and output as an XML document.
#
# See gr_monitor license in the gr_monitor.1 man page for licensing.
#
# michael@actrix.gen.nz, Nov 1999 (for Gentoo South Pacific Limited).
#

def printdtd():
    print '''<?xml version="1.0"?>
    <!DOCTYPE datastream [
    <!ELEMENT datastream (dataset)*>
    <!ELEMENT dataset (group)*>
    <!ATTLIST dataset
              id      CDATA   #REQUIRED>
    <!ELEMENT group (item)*>
    <!ATTLIST group
              id      CDATA   #REQUIRED>
    <!ELEMENT item (uptime, load1, load5, load15)>
    <!ATTLIST item
              id      CDATA   #REQUIRED>
    <!ELEMENT load1 EMPTY>
    <!ATTLIST load1
              id      CDATA   #IMPLIED
              data    CDATA   #REQUIRED>
    <!ELEMENT load5 EMPTY>
    <!ATTLIST load5
              id      CDATA   #IMPLIED
              data    CDATA   #REQUIRED>
    <!ELEMENT load15 EMPTY>
    <!ATTLIST load15
              id      CDATA   #IMPLIED
              data    CDATA   #REQUIRED>
    ]>'''

def printData():

    when = time.asctime(time.localtime(time.time()))
    rupInput = os.popen("rup -d", "r");
    data = []
    for line in rupInput.readlines(): 
	maxLoad = 0.0
	maxHost = "none"
	cols = string.split(line)
	(host, hostTime) = cols[0:2]
	host = host[:string.find(host, ".")]
	(load1, load5, load15) = map(float, cols[-3:])
	for load in (load1, load5, load15):
	    if (load > maxLoad):
		maxLoad = load
		maxHost = host
	data.append((host, hostTime, load1, load5, load15))
    rupInput.close()
    print '<dataset id="Load Averages (max = %3.1f on %s) %s">' % (maxLoad, maxHost, when)
    if (maxLoad < 3):
	maxLoad = 3;
    for item in data:
	(host, hostTime, load1, load5, load15) = item
	print '<group id="%s">' % (host)
	print '''
	<item id="%s">
	  <load1 id="1 m" data="%f"/>
	  <load5 id="5 m" data="%f"/>
	  <load15 id="15 m" data="%f"/>
	</item>
	''' % ( hostTime, 
		100.0 * load1 / maxLoad,
		100.0 * load5 / maxLoad, 
		100.0 * load15 / maxLoad )
	print '</group>'
    print '</dataset>'


# Main Program starts here 

if len(sys.argv) > 1 and sys.argv[1] == "-sleep":
    sleepSecs = int(sys.argv[2])
else:
    sleepSecs = 5

try:
    printdtd()
    print "<datastream>"
    
    while 1:
	printData()
	sys.stdout.flush()
	time.sleep(sleepSecs)
    
    print "</datastream>"
except IOError:
    sys.exit(-1)