import repository from arizona
[raven.git] / lib / arizona-lib / arizonawarning.py
1 #! /usr/bin/env python
2
3 """
4 <Program Name>
5    storkphonehome.py
6
7 <Started>
8    Nov 05, 2007
9
10 <Author>
11    Scott Baker
12
13 <Purpose>
14    Support for lightweight error messages / warnings. Errors are reported via
15    a http "GET" request to stork-repository.
16
17    NOTE: This code was taken from tools/stork/phonehome.py. A more complete
18    version of this code lives there.
19 """
20
21 import socket
22 import time
23 import sys
24 import os
25 import arizonaconfig
26 import arizonageneral
27
28
29
30
31 #           [option, long option,               variable,          action,        data, default, metavar, description]
32 """arizonaconfig
33    options=[
34             ["",     "--warningurl",            "warningurl",      "store",       "string", None,    None,    "URL to use when logging warnings"]]
35    includes=[]
36 """
37
38
39
40
41 def get_file_modify_time(fn):
42    """ return last-modify-time of a file """
43    try:
44        return os.stat(fn)[8]
45    except OSError:
46        return -1
47
48
49
50
51
52 def get_log_time(fn):
53    """
54    Search for initscript log files in /usr/local/stork/var/log/initscript
55    and /tmp. Return the modify time of the newest log file.
56    """
57    t1 = -1
58    t2 = -1
59    fn1 = os.path.join("/usr/local/stork/var/log/initscript", fn)
60    if os.path.exists(fn1):
61        t1 = get_file_modify_time(fn)
62    fn2 = os.path.join("/tmp", fn)
63    if os.path.exists(fn2):
64        t2 = get_file_modify_time(fn2)
65    return max(t1, t2)
66
67
68 def phonehome(name, status=0):
69    # get the time when the machine was booted
70    timeBoot = get_file_modify_time("/var/log/boot.log")
71
72    # get the time when the initscript was started
73    timeInitScriptStart = get_log_time("stork_initscript_stage1.log")
74
75    # get the time when the initscript installed the packages, before running
76    # stork for the first time
77    timeInitScriptPreRunStork = get_log_time("stork_initscript_stage2_prerunstork.log")
78
79    # get the time when the initscript was completed
80    timeInitScriptFinish = get_log_time("stork_initscript_stage2_complete.log")
81
82    slicename = arizonageneral.getslicename()
83    if not slicename:
84       slicename = "unknownslicename"
85
86    # some of these don't really make sense here; they were used for the
87    # arizona_stork_install tests
88    retryCount = 0
89    timeLastInstall = 0
90    timeTestStart = time.time()
91    timeReport = time.time()
92
93    try:
94        warningUrl = arizonaconfig.get_option("warningurl")
95    except:
96        warningUrl = None
97
98    try:
99        status = str(status).replace('"', "'").replace(" ", "_")
100    except:
101        pass
102
103    if not warningUrl:
104        warningUrl = "http://stork-repository.cs.arizona.edu/phonehome/phonehome.php"
105
106    cmd = 'curl "' + warningUrl + \
107           "?name=" + str(name) + \
108           "&slice=" + str(slicename) + \
109           "&host=" + str(socket.gethostname()) + \
110           "&timeBoot=" + str(int(timeBoot)) + \
111           "&timeNow=" + str(int(timeTestStart)) + \
112           "&retryCount=" + str(retryCount) + \
113           "&timeLastInstall=" + str(int(timeLastInstall)) + \
114           "&timeReport=" + str(int(timeReport)) + \
115           "&status=" + str(status) + \
116           "&timeInitScriptStart=" + str(int(timeInitScriptStart)) + \
117           "&timeInitScriptPreRunStork=" + str(int(timeInitScriptPreRunStork)) + \
118           "&timeInitScriptFinish=" + str(int(timeInitScriptFinish)) + \
119           "&filenameSuffix=storkwarning" + \
120           '"'
121
122    os.system(cmd)
123
124
125
126
127
128 def log_warning(name, status=0):
129    try:
130       phonehome(name, status)
131
132    except: # leave this as general exception; we don't want to crash in this code
133       pass
134