import repository from arizona
[raven.git] / tools / statuscheck / statuscheck_remote.py
1 #! /usr/bin/python
2
3 """
4 <Program Name>
5    statuscheck_remote.py
6
7 <Started>
8    November, 2007
9
10 <Author>
11    Scott Baker
12
13 <Purpose>
14    Status Check tool for planetlab nodes. This is the remote part of the status
15    checker.
16 """
17
18 import getopt
19 import os
20 import statvfs
21 import sys
22 import tempfile
23 import time
24
25
26
27
28
29 def check_disk(fn):
30    """
31    <Purpose>
32       Check a disk for free space.
33
34    <Arguments>
35       fn
36          name of a file on the disk to be checked
37
38    <Exceptions>
39       None
40
41    <Returns>
42       None
43    """
44    try:
45       result = os.statvfs(fn)
46
47       bsize = result[statvfs.F_BSIZE]
48       blocks = result[statvfs.F_BLOCKS]
49       blocks_avail = result[statvfs.F_BAVAIL]
50       files = result[statvfs.F_FILES]
51       files_avail = result[statvfs.F_FAVAIL]
52
53       if blocks > 0:
54          blockpct = 100 - (blocks_avail * 100 / blocks)
55       else:
56          blockpct = -1
57
58       if files > 0:
59          filepct = 100 - (files_avail * 100 / files)
60       else:
61          filepct = -1
62
63       print "resource blocks ", blockpct
64       print "resource files ", filepct
65
66       if blockpct >= 99:
67          print "error check_disk almost_full_blocks"
68
69       if filepct >= 99:
70          print "error check_disk almost_full_files"
71
72    except Exception, e:
73       print "error check_disk", str(e).replace(" ","_")
74
75
76
77
78
79 def check_mktemp():
80    """
81    <Purpose>
82       Check availability of the temp directory by creating a temp file
83
84    <Arguments>
85       None
86
87    <Exceptions>
88       None
89
90    <Returns>
91       None
92    """
93    try:
94       (filedes, filename) = tempfile.mkstemp("statuscheck")
95       os.close(filedes)
96       os.remove(filename)
97    except Exception, e:
98       print "error check_temp", str(e).replace(" ","_")
99
100
101
102
103
104 def count_dir(fn):
105    """
106    <Purpose>
107       Count the directory entries in a directory.
108
109    <Arguments>
110       fn
111          name of directory to count
112
113    <Exceptions>
114       None
115
116    <Returns>
117       None
118    """
119    try:
120       files = os.listdir(fn)
121       count = len(files)
122
123       print "count", fn, count
124    except Exception, e:
125       print "error count_dir", str(e).replace(" ","_")
126
127
128
129
130
131 def check_pid(pid):
132    """
133    <Purpose>
134       Check to see if a process id is running
135
136    <Arguments>
137       pid
138          process id
139
140    <Exceptions>
141       None
142
143    <Returns>
144       True if pid is running
145       False otherwise
146    """
147    return (os.system("ps -p " + str(pid) + " > /dev/null") == 0)
148
149
150
151
152
153 def check_daemon(fn, name):
154    """
155    <Purpose>
156       Check to see if a daemon is running by reading its pid file and seeing
157       if the process is running.
158
159    <Arguments>
160       fn
161          filename of pid file
162       name
163          name of daemon
164
165    <Exceptions>
166       None
167
168    <Returns>
169       True if daemon running
170       False otherwise
171    """
172    try:
173       try:
174          file = open(fn)
175       except IOError:
176          print "daemon " + name + " not_running"
177          return False
178       except OSError:
179          print "daemon " + name + " not_running"
180          return False
181
182       try:
183          pid_str = file.readline()
184       except IOError:
185          print "daemon " + name + " bad_pid_file"
186          return False
187
188       file.close()
189
190       try:
191          pid = int(pid_str)
192       except TypeError:
193          print "daemon " + name + " bad_pid_string"
194          return False
195
196       if check_pid(pid):
197          print "daemon " + name + " running"
198          return True
199       else:
200          print "daemon " + name + " not_running"
201          return False
202
203    except Exception, e:
204       print "daemon " + name, str(e).replace(" ","_")
205       return False
206
207
208
209
210
211 def create_pid_file(name):
212    """
213    <Purpose>
214       Create file holding the process id
215
216    <Arguments>
217       name
218          filename of pid file
219
220    <Exceptions>
221       None
222
223    <Returns>
224       None
225    """
226    try:
227       file = open(name, "w")
228       file.write(str(os.getpid()))
229       file.close()
230    except:
231       print "error statuscheck_remote create_pid_file"
232
233
234
235
236
237 def delete_pid_file(name):
238    """
239    <Purpose>
240       Delete the pid file
241
242    <Arguments>
243       name
244          filename of pid file
245
246    <Exceptions>
247       None
248
249    <Returns>
250       None
251    """
252    try:
253       os.remove(name)
254    except:
255       print "error statuscheck_remote delete_pid_file"
256
257
258
259
260
261 def check_package(name):
262    """
263    <Purpose>
264       Get the installed versions of a package.
265
266    <Arguments>
267       name
268          name of package to check
269
270    <Exceptions>
271       None
272
273    <Returns>
274       True if some version is installed
275       False otherwise
276    """
277    try:
278       cmd_in = os.popen("rpm -q " + name)
279
280       result = []
281
282       output = cmd_in.readline().strip()
283       while output:
284          if not output.endswith("not installed"):
285              result.append(output)
286          output = cmd_in.readline().strip()
287
288       cmd_in.close()
289
290       if not result:
291          print "package " + name + " not_installed"
292          return False
293
294       if len(result) == 1:
295          print "package " + name + " " + result[0]
296       else:
297          print "package " + name + " " + str(result)
298
299       return True
300
301    except:
302       print "package " + name + " uncaught_exception_while_checking"
303       return False
304
305
306 def reset_benchmark(package):
307 #   os.system("rm -rf /tmp/statuscheck_bench_out")
308 #   os.system("rm -rf /usr/local/stork/var/packageinfo/*")
309    if package:
310       if package=="balsa":
311          deps = "libsmtp Glide3 XFree86-base XFree86-libs XFree86-libs-data XFree86-xfs XFree86 linc libxml libgnomeprint libgnomelprintui pspell"
312       elif package=="XFree86":
313          deps = "xorg-x11-Mesa-libGL xorg-x11-libs xorg-x11-font-utils xorg-x11-xauth xorg-x11-xfs chkfontpath Glide3 XFree86-base-fonts XFree86-libs-data XFree86-libs XFree86-xfs fonts-xorg-base xorg-x11 xterm xinitrc"
314       elif package=="java-1.4.2-gcj-compat":
315          deps = "atk freetype xorg-x11-Mesa-libGL fontconfig xorg-x11-libs libjpeg pango libpng xorg-x11-Mesa-libGLU freeglut libtiff gtk2 libart_lgpl zip libgcj gjdoc gnu-crypto-sasl-jdk1.4 gnu-crypto-jce-jdk1.4 jpackage-utils gnu-crypto jessie"
316       else:
317          deps = ""
318
319       os.system("rpm -e " + package + " " + deps + " >> /tmp/statuscheck_bench_out  2>&1")
320       os.system("rm -f /tmp/*.rpm")
321
322 def run_benchmark(package, version=None, extra=None, protocol=None,
323                  disable_tp=False,
324                  disable_packsig=False,
325                  disable_tphash=False,
326                  disable_signed_metafile=False,
327                  disable_transferhashcheck=False,
328                  disable_xmlsigcheck=False,
329                  disable_metadatavalidation=False):
330    tag = package
331
332    command = "stork"
333
334    # something really big to prevent cached metafiles
335    command = command + " --metafilecachetime 0"
336
337    command = command + ' --repositorykey "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAN5k8fLj76b5N03XmQtlYc+XayJEXTIRzI5S1tVuig8mKyQL+qAHboCCvCzZy3lM+m5zY95JD8ZAXI1s65i0pasCAwEAAQ"'
338
339    if extra:
340       tag = tag + "_" + extra
341
342    if protocol:
343       command = command + ' --transfermethod "##reset##" --transfermethod ' + protocol
344       tag = tag + "_"+protocol
345
346    if disable_packsig:
347       command = command + " --disablepacksig"
348       tag = tag + "_dpacksig"
349
350    if disable_tp:
351       command = command + " --disabletrustedpackagesdata"
352       tag = tag + "_dtp"
353
354    if disable_transferhashcheck:
355       command = command + " --disabletransferhashcheck"
356       tag = tag + "_dtransferhash"
357
358    if disable_signed_metafile:
359       command = command + " --disablesignedmetafile"
360       tag = tag + "_dsignedmeta"
361
362    if disable_xmlsigcheck:
363       command = command + " --disablexmlsigcheck"
364       tag = tag + "_dxmlsig"
365
366    if disable_metadatavalidation:
367       command = command + " --disablemetadatavalidation"
368       tag = tag + "_dmetaval"
369
370    if disable_tphash:
371       command = command + " --disabletphash"
372       tag = tag + "_dtphash"
373
374    command = command + " " + package
375
376    if version:
377       command = command + "=" + version
378
379    command = command + " >> /tmp/statuscheck_bench_out  2>&1"
380
381    # print "running", command
382
383    tstart = time.time()
384    result = os.system(command)
385    tstop = time.time()
386
387    print "bench_result", tag, result
388    if result == 0:
389       print "bench_time", tag, tstop-tstart
390
391    print "bench_start", tag, tstart
392    print "bench_stop", tag, tstop
393
394    return result
395
396 def run_benchmarks2():
397    package = "XFree86" #"java-1.4.2-gcj-compat"
398    version = None
399
400    # if stork is not installed, do nothing
401    if not os.path.exists("/usr/local/stork/bin/stork.py"):
402       return
403
404    reset_benchmark(package)
405    result = run_benchmark(package, version, extra="prep", protocol="nest")
406    if (result != 0):
407       return
408
409    reset_benchmark(package)
410    run_benchmark(package, version, protocol="http")
411
412    reset_benchmark(package)
413    run_benchmark(package, version, protocol="nest")
414
415 def run_benchmarks(package, version=None):
416    # if stork is not installed, do nothing
417    if not os.path.exists("/usr/local/stork/bin/stork.py"):
418       return
419
420    reset_benchmark(package)
421    run_benchmark(package, version, protocol="http", disable_packsig=True, disable_tp=True, disable_transferhashcheck=True, disable_signed_metafile=True, disable_xmlsigcheck=True, disable_metadatavalidation=True)
422
423    reset_benchmark(package)
424    run_benchmark(package, version, protocol="http",                       disable_tp=True, disable_transferhashcheck=True, disable_signed_metafile=True, disable_xmlsigcheck=True, disable_metadatavalidation=True)
425
426    reset_benchmark(package)
427    run_benchmark(package, version, protocol="http",                                        disable_transferhashcheck=True, disable_signed_metafile=True, disable_xmlsigcheck=True, disable_metadatavalidation=True)
428
429    reset_benchmark(package)
430    run_benchmark(package, version, protocol="https",                                       disable_transferhashcheck=True, disable_signed_metafile=True, disable_xmlsigcheck=True, disable_metadatavalidation=True)
431
432    reset_benchmark(package)
433    run_benchmark(package, version, protocol="https",                                                                       disable_signed_metafile=True, disable_xmlsigcheck=True, disable_metadatavalidation=True)
434
435    reset_benchmark(package)
436    run_benchmark(package, version, protocol="https",                                                                                                     disable_xmlsigcheck=True, disable_metadatavalidation=True)
437
438    reset_benchmark(package)
439    run_benchmark(package, version, protocol="https",                                                                                                                               disable_metadatavalidation=True)
440
441    reset_benchmark(package)
442    run_benchmark(package, version, protocol="https")
443
444 def main():
445    bench = False
446    bench2 = False
447    passes = 1
448
449    (options, args) = getopt.getopt(sys.argv[1:], '', ["passes=","bench","bench2"])
450    for opt in options:
451        optname = opt[0]
452
453        if optname == "--passes":
454           passes = int(opt[1])
455        elif optname == "--bench":
456           bench = True
457        elif optname == "--bench2":
458           bench2 = True
459
460    # if we are already running, then don't run again
461    if check_daemon("/var/run/stork_statuscheck.pid", "statuscheck"):
462        return
463
464    create_pid_file("/var/run/stork_statuscheck.pid")
465
466    check_disk("/tmp")
467    check_mktemp()
468
469    check_package("arizona-lib")
470    check_package("stork-client")
471    check_daemon("/var/run/stork_initscript.pid", "initscript")
472    check_daemon("/var/run/pacmand.pid", "pacmand")
473
474    check_package("stork-nestproxy")
475    check_daemon("/var/run/stork_nest_proxy.py.pid", "stork_nest_proxy")
476
477    # python's listdir can sometimes be very slow with large directories,
478    # so count the size of /tmp last
479    count_dir("/tmp")
480
481    if bench:
482       os.system("rm -rf /tmp/statuscheck_bench_out")
483       for i in range(passes):
484          run_benchmarks("XFree86", "4.3.0-2") # "gnuchess", "5.02-11")
485
486    if bench2:
487       os.system("rm -rf /tmp/statuscheck_bench_out")
488       run_benchmarks2()
489
490    delete_pid_file("/var/run/stork_statuscheck.pid")
491
492 if __name__ == "__main__":
493    main()
494