-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnag_external_commands.py
executable file
·130 lines (120 loc) · 3.61 KB
/
nag_external_commands.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
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python
from BeautifulSoup import BeautifulSoup
import urllib2
import re, htmlentitydefs
def unescape(text):
"""
Removes HTML or XML character references
and entities from a text string.
keep &, >, < in the source code.
from Fredrik Lundh
http://effbot.org/zone/re-sub.htm#unescape-html
slightly modified
"""
def fixup(m):
text = m.group(0)
if text[:2] == "&#":
# character reference
try:
if text[:3] == "&#x":
return unichr(int(text[3:-1], 16))
else:
return unichr(int(text[2:-1]))
except ValueError:
#print "erreur de valeur"
pass
else:
# named entity
try:
#print text[1:-1]
text = unichr(htmlentitydefs.name2codepoint[text[1:-1]])
except KeyError:
#print "keyerror"
pass
return text # leave as is
return re.sub("&#?\w+;", fixup, text)
def wrap(txt, ind='', cols=80):
"""
wraps txt (line of text) at cols columns
"""
word = ''
col = 0
line = ''
char = ''
if txt[-1] == "\n":
txt2 = txt[0:-1]
else:
txt2 = txt
for char in txt2:
if not char.isspace():
word += char
else:
if word:
if col + len(word) <= cols or not line:
line += word
col += len(word)
else:
line = line.rstrip() + "\n" + ind + word
#col = len(ind + word)
col = len(word)
word = ""
if char != '\n':
line += char
col += 1
elif line[-1] != ' ':
line += ' '
col += 1
if word:
if col + len(word) < cols:
line += word
else:
line = line.rstrip() + "\n" + ind + word
if txt[-1] == '\n':
line += "\n"
return line
def cmd2py(cmd, descr):
cmd = cmd.strip().replace('<', '').replace('>', '')
descr = descr.strip()
c = cmd.split(';')
name = c[0].lower()
args = ', '.join(c[1:])
method = " def %s(self, %s):\n" % (name, args) + \
' """\n' + \
' %s\n' % wrap(descr, ind=' ') + \
' """\n' + \
" self.run('%s', %s)\n" % (c[0], args)
return method
if __name__ == '__main__':
root_url = 'http://old.nagios.org/developerinfo/externalcommands/'
f = urllib2.urlopen(root_url + 'commandlist.php')
soup = BeautifulSoup(f.read())
f.close()
content_table = soup.find('table', { 'class' : 'Content' })
hrefs = content_table.findAll('a')[4:]
for a in hrefs:
#print a['href']
#print a.string
f = urllib2.urlopen(root_url + a['href'])
s = BeautifulSoup(f.read())
t = s.find('table', { 'class': 'Content' })
tds = t.findAll('td')
p = False
cmd = ''
descr = ''
set_cmd = set_descr = False
for td in tds:
if set_cmd:
cmd = unescape(td.string.decode())
set_cmd = False
elif set_descr:
descr = unescape(td.string.decode())
set_descr = False
if td.string == 'Command Format:':
set_cmd = True
elif td.string == 'Description:':
set_descr = True
if cmd and descr:
break
if cmd and descr:
print cmd2py(cmd, descr)
f.close()