summaryrefslogtreecommitdiffstats
path: root/Monitoring/MonitoringService/Example/Tools.py
blob: d842d042e2cd33f8cea499e7f96bda98235ad08e (plain)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
'''
Created on Oct 12, 2011

@author: steger
@summary: Here we define some monitoring tools and dress them up with parameters and work flow description
'''
from DataProcessing.Parameter import Parameter, ParameterList
from Example.Resources import PLdict
from Credential.credentialtypes import UsernamePassword, UsernameRSAKey
from Driver.SOAPClient import SOAPClient
from Driver.SshExec import SshExec
from Example.Units import UM, Byte, micro_second, piece, milli_second,\
    nano_second, unitless, nano_unixtimestamp, unixtimestamp, fraction,\
    kilo_Byte, second
from Example.Dimensions import cardinal, countable, ipaddress, timeinterval,\
    informationsize, pointintime, nameofsomething, probability
from DataProcessing.DataHeader import DataHeaderGeneratedByDescription

DOM_SUBSTRATE = 1
DOM_SLICE = 2

sonoma_url = "http://complex.elte.hu/~steger/sonoma/user.wsdl"

nodes = map(lambda x:(x.get_hostname("eth0"), unitless), PLdict.values())

class sonomashortping:
    driver = SOAPClient
    name = "SONoMAPing"
    domain = DOM_SUBSTRATE
    dataheaderdeclaration = DataHeaderGeneratedByDescription('ping', [('Run', countable), 
                                                ('Sequence Number', countable), 
                                                ('Source Address', ipaddress), 
                                                ('Destination Address', ipaddress), 
                                                ('Packet Size', informationsize, Byte), 
                                                ('Time To Live', countable), 
                                                ('Round Trip Delay', timeinterval, micro_second)])    
    
    authtype = (UsernamePassword, )
    kwargs = { "url": sonoma_url, "MAserviceport": 11123 }
    hooks = {
        "prehook" : """
from base64 import b64decode
self.decode = b64decode
self.pattern = re.compile('^(\d+)\s+(\d+\.\d+\.\d+\.\d+)\s+(\d+\.\d+\.\d+\.\d+)\s+(\d+)\s+(\d+)\s+(\d+)$')
self.username=self.credential.username
self.password=self.credential.password
self.client = self.driver(kw.get('url'))
self.sessionId = self.client.service.requestSession(self.username, self.password, 'CSV', False)
self.port = kw.get('MAserviceport')
self.template = self.data.getTemplate(size = 1)
        """,
        "retrievehook" : """
source = "%s:%d" % (self.parameters.get('SourceAddress', self.um.ipv4dotted), self.port)
res = self.client.service.shortPing(self.sessionId, 
  source, self.parameters.get('DestinationAddress', self.um.ipv4dotted), self.parameters.get('Count', self.um.piece), 
  self.parameters.get('Delay', self.um.micro_second), self.parameters.get('PacketSize', self.um.Byte))
rec = self.decode(res).splitlines()
for r in rec:
    if self.pattern.match(r):
        self.template.clear()
        ex = self.pattern.split(r)[:-1]
        ex[0] = self.runcount
        self.template.updateMany( ('Run', 'Sequence Number', 'Source Address', 'Destination Address', 'Packet Size', 'Time To Live', 'Round Trip Delay'), [ex,] )
        self.data.saveRecord(self.template)
return True
        """,
        "posthook": "self.client.service.closeSession(self.username, self.password, self.sessionId)"}
    parameters = ParameterList([ Parameter(name = "SourceAddress", valuetype = str, unitmanager = UM, dimension = ipaddress),
                   Parameter(name = "DestinationAddress", valuetype = str, unitmanager = UM, dimension = ipaddress),
                   Parameter(name = "Count", valuetype = int, unitmanager = UM, dimension = countable, default = (5, piece)),
                   Parameter(name = "Delay", valuetype = int, unitmanager = UM, dimension = timeinterval, default = (100, milli_second)),
                   Parameter(name = "PacketSize", valuetype = int, unitmanager = UM, dimension = informationsize, default = (64, Byte)) ])

class sonomashortchirp:
    driver = SOAPClient
    name = "SONoMAChirp"
    domain = DOM_SUBSTRATE
    dataheaderdeclaration = DataHeaderGeneratedByDescription('onewaydelay', [('Run', countable), 
                                                ('SequenceNumber', countable), 
                                                ('SourceAddress', ipaddress), 
                                                ('DestinationAddress', ipaddress), 
                                                ('TimestampSend', pointintime, nano_unixtimestamp), 
                                                ('OnewayDelay', timeinterval, nano_second) ])    
    authtype = (UsernamePassword, )
    kwargs = { "url": sonoma_url, "MAserviceport": 11123 }
    hooks = {
        "prehook" : """
from base64 import b64decode
self.decode = b64decode
self.pattern = re.compile('^(\d+)\s+(\d+\.\d+\.\d+\.\d+)\s+(\d+\.\d+\.\d+\.\d+)\s+(\d+)\s+(\d+)$')
self.username=self.credential.username
self.password=self.credential.password
self.client = self.driver(kw.get('url'))
self.sessionId = self.client.service.requestSession(self.username, self.password, 'CSV', False)
self.port = kw.get('MAserviceport')
self.template = self.data.getTemplate(size = 1)
self.delaylist = self.client.factory.create("delayList")
self.delaylist.gap = [100000,100000]
        """,
        "retrievehook" : """
source = "%s:%d" % (self.parameters.get('SourceAddress', self.um.ipv4dotted), self.port)
destination = "%s:%d" % (self.parameters.get('DestinationAddress', self.um.ipv4dotted), self.port)
res = self.client.service.shortChirp(self.sessionId, 
  source, self.parameters.get('SourcePort', self.um.unitless), 
  destination, self.parameters.get('DestinationPort', self.um.unitless),
  self.parameters.get('Count', self.um.piece), self.parameters.get('Delay', self.um.milli_second), 
  self.parameters.get('PacketSize', self.um.Byte), self.delaylist)
rec = self.decode(res).splitlines()
data = []
for r in rec:
    if self.pattern.match(r):
        self.template.clear()
        ex = self.pattern.split(r)[:-1]
        ex[0] = self.runcount
        ex[-1] = int(ex[-1])-int(ex[-2])
        data.append( ex )
self.template.clear(size = len(data))
self.template.updateMany( ('Run', 'SequenceNumber', 'SourceAddress', 'DestinationAddress', 'TimestampSend', 'OnewayDelay'), data )
self.data.saveRecord(self.template)
return True
        """,
        "posthook": "self.client.service.closeSession(self.username, self.password, self.sessionId)"}
    parameters = ParameterList([ Parameter(name = "SourceAddress", valuetype = str, unitmanager = UM, dimension = ipaddress),
                   Parameter(name = "DestinationAddress", valuetype = str, unitmanager = UM, dimension = ipaddress),
                   Parameter(name = "Count", valuetype = int, unitmanager = UM, dimension = countable, default = (5, piece)),
                   Parameter(name = "Delay", valuetype = int, unitmanager = UM, dimension = timeinterval, default = (100, milli_second)),
                   Parameter(name = "PacketSize", valuetype = int, unitmanager = UM, dimension = informationsize, default = (64, Byte)),
                   Parameter(name = "SourcePort", valuetype = int, unitmanager = UM, dimension = cardinal, default = (7777, unitless)),
                   Parameter(name = "DestinationPort", valuetype = int, unitmanager = UM, dimension = cardinal, default = (7777, unitless)), ])


class sshping:
    driver = SshExec
    name = "sshPing"
    domain = DOM_SLICE
    dataheaderdeclaration = DataHeaderGeneratedByDescription('ping', [('Run', cardinal), 
                                                ('TimeReceived', pointintime), 
                                                ('PacketSize', informationsize), 
                                                ('DestinationAddress', ipaddress), 
                                                ('SequenceNumber', countable), ('TimeToLive', countable), 
                                                ('RoundTripDelay', timeinterval, milli_second)])
    authtype = (UsernameRSAKey, UsernamePassword)  
    kwargs = {} 
    hooks = {
        "prehook" : """
self.pattern = re.compile('^\[(\d+\.?\d*)\]\s*(\d+)\s*bytes\s*from\s*(\d+\.\d+\.\d+\.\d+):\s*icmp_req=(\d+)\s*ttl=(\d+)\s*time=(\d+\.?\d*)\s*(\w*)')
self.template = self.data.getTemplate(size = self.parameters.get('Count', self.um.piece))
command = "ping -D -n -c %d -i %f -t %d -I %s %s" % (
    self.parameters.get('Count', self.um.piece), self.parameters.get('Delay', self.um.second), 
    self.parameters.get('TimeToLive', self.um.piece), self.parameters.get('Interface', self.um.unitless), 
    self.parameters.get('DestinationAddress', self.um.ipv4dotted))
self.client = self.driver(host = self.parameters.get('SourceAddress', self.um.ipv4dotted), credential = self.credential, command = command)
        """,
        "retrievehook" : """
data = []
for r in self.client.execute().readlines():
    if self.pattern.match(r):
        ex = self.pattern.split(r)[:-2]
        ex[0] = self.runcount
        data.append( ex )
self.template.clear(size = len(data))
self.template.updateMany( ('Run', 'TimeReceived', 'PacketSize', 'DestinationAddress', 'SequenceNumber', 'TimeToLive', 'RoundTripDelay'), data )
self.data.saveRecord(self.template)
return True
        """}
    parameters = ParameterList([ Parameter(name = "SourceAddress", valuetype = str, unitmanager = UM, dimension = ipaddress),
                   Parameter(name = "DestinationAddress", valuetype = str, unitmanager = UM, dimension = ipaddress),
                   Parameter(name = "Count", valuetype = int, unitmanager = UM, dimension = countable, default = (5, piece)),
                   Parameter(name = "Delay", valuetype = float, unitmanager = UM, dimension = timeinterval, default = (200, milli_second)), 
                   Parameter(name = "TimeToLive", valuetype = int, unitmanager = UM, dimension = countable, default = (32, piece)), 
                   Parameter(name = "Interface", valuetype = str, unitmanager = UM, dimension = nameofsomething, default = ("eth0", unitless)) ] )

class sshmeminfo:
    driver = SshExec
    name = "sshMeminfo"
    domain = DOM_SLICE | DOM_SUBSTRATE
    dataheaderdeclaration = DataHeaderGeneratedByDescription('meminfo', [('Run', cardinal), 
                                                   ('AvailableMemory', informationsize), 
                                                   ('FreeMemory', informationsize)])
    authtype = (UsernameRSAKey, UsernamePassword)  
    kwargs = {} 
    hooks = {
        "prehook" : """
self.pattern = re.compile('^(.*):\s*(\d+)\s+(.B)$')
self.template = self.data.getTemplate(size = 1)
command = "cat /proc/meminfo"
self.client = self.driver(host = self.parameters.get('SourceAddress', self.um.ipv4dotted), credential = self.credential, command = command)
        """,
        "retrievehook" : """
self.template.clear()
self.template.update('Run', (self.runcount,))
for r in self.client.execute().readlines():
    if self.pattern.match(r):
        n, v, u = self.pattern.split(r)[1:-1]
        if n == 'MemTotal' and u == 'kB':
            self.template.update('AvailableMemory', (v,))
        elif n == 'MemFree' and u == 'kB':
            self.template.update('FreeMemory', (v,))
self.data.saveRecord(self.template)
return True
        """}
    parameters = ParameterList([ Parameter(name = "SourceAddress", valuetype = str, unitmanager = UM, dimension = ipaddress), ])

class sshdf:
    driver = SshExec
    name = "sshDiskinfo"
    domain = DOM_SLICE | DOM_SUBSTRATE
    dataheaderdeclaration = DataHeaderGeneratedByDescription('diskinfo', [('Run', cardinal), 
                                                   ('Available', informationsize, kilo_Byte), 
                                                   ('Used', informationsize, kilo_Byte)])
    authtype = (UsernameRSAKey, UsernamePassword)  
    kwargs = {} 
    hooks = {
        "prehook" : """
self.pattern = re.compile('^.*\s+\d+\s+(\d+)\s+(\d+)\s+\d+%\s+.*$')
self.template = self.data.getTemplate(size = 1)
command = "df %s" % self.parameters.get('Directory', self.um.unitless)
self.client = self.driver(host = self.parameters.get('SourceAddress', self.um.ipv4dotted), credential = self.credential, command = command)
        """,
        "retrievehook" : """
self.template.clear()
self.template.update('Run', (self.runcount,))
for r in self.client.execute().readlines():
    if self.pattern.match(r):
        u, a = self.pattern.split(r)[1:-1]
        self.template.update('Available', (a,))
        self.template.update('Used', (u,))
self.data.saveRecord(self.template)
return True
        """}
    parameters = ParameterList([ 
            Parameter(name = "SourceAddress", valuetype = str, unitmanager = UM, dimension = ipaddress), 
            Parameter(name = "Directory", valuetype = str, unitmanager = UM, dimension = nameofsomething), 
        ])

    
class sshtraceroute:
    driver = SshExec
    name = "sshTraceroute"
    domain = DOM_SLICE
    dataheaderdeclaration = DataHeaderGeneratedByDescription('traceroute', [('Run', cardinal),
                                                      ('Hop', countable), 
                                                      ('Raw', nameofsomething)])
    authtype = (UsernameRSAKey, UsernamePassword)  
    kwargs = {} 
    hooks = {
        "prehook" : """
self.pattern = re.compile('^\s*(\d+)\s+(.*)$')
self.template = self.data.getTemplate(size = 1)
command = "traceroute -n %s" % (self.parameters.get('DestinationAddress', self.um.ipv4dotted))
self.client = self.driver(host = self.parameters.get('SourceAddress', self.um.ipv4dotted), credential = self.credential, command = command)
        """,
        "retrievehook" : """
data = []
for r in self.client.execute().readlines():
    if self.pattern.match(r):
        ex = self.pattern.split(r)[:-1]
        ex[0] = self.runcount
        data.append( ex )
self.template.clear(size = len(data))
self.template.updateMany( ('Run', 'Hop', 'Raw'), data )
self.data.saveRecord(self.template)
return True
        """}
    parameters = ParameterList([ Parameter(name = "SourceAddress", valuetype = str, unitmanager = UM, dimension = ipaddress),
                   Parameter(name = "DestinationAddress", valuetype = str, unitmanager = UM, dimension = ipaddress),
                   Parameter(name = "Count", valuetype = int, unitmanager = UM, dimension = countable, default = (5, piece)), ])
    
class sshhades:
    driver = SshExec
    name = "HADESaggregates"
    domain = DOM_SUBSTRATE
    dataheaderdeclaration = DataHeaderGeneratedByDescription('hadestable',  [('Run', cardinal),
                                                       ('Time', pointintime, unixtimestamp), 
                                                       ('MinDelay', timeinterval, second), 
                                                       ('MedianDelay', timeinterval, second), 
                                                       ('MaxDelay', timeinterval, second), 
                                                       ('Loss', probability, fraction),
                                                       ])
    authtype = (UsernameRSAKey, UsernamePassword)  
    kwargs = { 'repository': '194.132.52.212', 'samplecount': 9 } 
    hooks = {
        "prehook" : """
self.repository = kw.get('repository')
self.pattern = re.compile('^(\d+)\s+(-?\d+\.?\d*)\s+(-?\d+\.?\d*)\s+(-?\d+\.?\d*)\s+(\d+)\s+.*$')
self.template = self.data.getTemplate(size = 1)
lookup = { '192.168.31.1': 'PSNC_FED', '192.168.31.5': 'DFN_FED', '192.168.31.9': 'GARR_FED' }
root = "/home/novi-monitoring"
source = lookup[ self.parameters.get('SourceAddress', self.um.ipv4dotted) ]
destination = lookup[ self.parameters.get('DestinationAddress', self.um.ipv4dotted) ]
lookupcommand = "echo %s/data/hades/novi/www/*/*/*/%s.%s.0.qos_ai.dat" % (root, source, destination)
self.client = self.driver(host = self.repository, credential = self.credential)
files = self.client.execute(lookupcommand).read().split()
self.command = "%s/hades/bin/hades-show-data.pl --config=novi %s"  % (root, files[-1])
self.nsamples = int(kw.get('samplecount'))
        """,
        "retrievehook" : """
data = []
for r in self.client.execute(self.command).readlines():
    print r
    if self.pattern.match(r):
        ts, dtmin, dtmed, dtmax, loss = self.pattern.split(r)[1:-1]
        data.append( [ self.runcount, ts, dtmin, dtmed, dtmax, float(loss)/self.nsamples ] )
self.template.clear(size = len(data))
self.template.updateMany( ('Run', 'Time', 'MinDelay', 'MedianDelay', 'MaxDelay', 'Loss'), data )
self.data.saveRecord(self.template)
return True
        """}
    parameters = ParameterList([ Parameter(name = "SourceAddress", valuetype = str, unitmanager = UM, dimension = ipaddress),
                   Parameter(name = "DestinationAddress", valuetype = str, unitmanager = UM, dimension = ipaddress),
                   ])