summaryrefslogtreecommitdiffstats
path: root/cobbler/action_status.py
blob: f288e7a372fb62e90ca8541a79cce44c20214405 (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
"""
Reports on kickstart activity by examining the logs in
/var/log/cobbler.

Copyright 2007, Red Hat, Inc
Michael DeHaan <mdehaan@redhat.com>

This software may be freely redistributed under the terms of the GNU
general public license.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
"""

import os
import time
import cobbler_msg

class BootStatusReport:

    def __init__(self,config,mode):
        """
        Constructor
        """
        self.config   = config
        self.settings = config.settings()
        self.mode     = mode

   # -------------------------------------------------------

    def run(self):
        """
        Calculate and print a kickstart-status report.
        For kickstart trees not in /var/lib/cobbler (or a symlink off of there)
        tracking will be incomplete.  This should be noted in the docs.
        """
 
        files = {} # keep up with xfer'd files by IP addr
        
        # gather stats from logfiles, allowing for rotations in the log file ...
        last_recorded_time = 0
        time_collisions = 0
        for i in range(0,6):
 
            # figure out what logfile to open
            fname = "/var/log/cobbler/cobbler.log"
            if i != 0:
               fname = "/var/log/cobbler/cobbler.log.%s" % (i)

            # open it if it's there
            if not os.path.exists(fname):
                # print "no such file: %s" % fname
                break
            logfile = open(fname, "r")
 
            # each line in the file corresponds to an accessed file
            while(True):
               data = logfile.readline()
               if data is None or data == "":
                   break

               # fields are tab delimited
               # (1) seconds since 1970, in decimal
               # (2) ASCII date for humans
               # (3) IP address of requester
               # (4) HTTP request line
       
               (epoch, strdate, ip, request) = data.split("\t")
              
               # HTTP request line is essentially space delimited
               # (1) method, which should always be GET
               # (2) filename, which is relative from server root
               # (3) protocol, such as HTTP/1.1
       
               (method, filepath, protocol) = request.split(" ")

               # make room in the nested datastructures for report info
        
               if not files.has_key(ip):
                  files[ip] = {} # hash of access times and filenames
 
               # keep track of when IP addresses's finish.  Right now, we don't
               # care much about profile or system name but can get this later.

               # we are storing times in a hash, and this prevents them from colliding
               # which would break the filecount and possibly the state check
               logtime = float(epoch)
               if int(logtime) == last_recorded_time:
                   time_collisions = time_collisions + 1
               else:
                   time_collisions = 0
               logtime = logtime + (0.001 * time_collisions)

               if filepath.find("/cobbler_track/") == -1:
                  # it's a regular cobbler path, not a cobbler_track one, so ignore it.
                  # the logger doesn't need to log these anyway, but it might in the future.
                  # this is just an extra safeguard.
                  pass
               elif filepath.find("?system_done") != -1:             
                  files[ip][logtime] = "DONE"
               elif filepath.find("?profile_done") != -1:
                  files[ip][logtime] = "DONE"
               else:
                  files[ip][logtime] = filepath

               last_recorded_time = int(logtime) 

               # FIXME: calculate start times for each IP as defined as earliest file
               # requested after each stop time, or the first file requested if no
               # stop time.

            logfile.close()


        # report on the data found in all of the files, aggregated.

        self.generate_report(files)
        return True

     #-----------------------------------------

    def generate_report(self,files):
        """
        Given the information about transferred files and kickstart finish times, attempt
        to produce a report that most describes the state of the system.

        FIXME: just text for now, but should pay attention to self.mode and possibly offer
        a HTML or XML or YAML version for other apps.  Not all, just some alternatives.
        """
        

        # find all machines that have logged kickstart activity
        ips = files.keys()
        ips.sort()

        # FIXME: print the header
        # ...
 
        # for each machine
        for ip in ips:

           # sort the access times
           rtimes = files[ip].keys()
           rtimes.sort()

           # variables for calculating kickstart state
           last_request_time = 0
           last_start_time = 0
           last_done_time = 0
           files_counter = 0
           install_state = "norecord"

           # for each request time the machine has made
           for rtime in rtimes:

               rtime = rtime
               fname = files[ip][rtime]

               if fname != "DONE":
                   # process kickstart done state
                   if install_state == "done" or install_state == "norecord":
                       files_counter = 0
                       last_start_time = rtime
                   install_state = "notdone"
                   last_request_time = rtime 
                   files_counter = files_counter + 1
               else:       
                   # kickstart finished
                   install_state = "done"
                   last_request_time = rtime
                   last_done_time = rtime

           # calculate elapsed time for kickstart
           elapsed_time = 0
           if install_state != "norecord":
               elapsed_time = last_request_time - last_start_time

           # FIXME: IP to MAC mapping where cobbler knows about it would be nice.

           display_start = time.asctime(time.localtime(last_start_time))
           display_last  = time.asctime(time.localtime(last_request_time))
 
           # print the status for this IP address
           print "%-20s | %-15s | %-20s | %-20s | %-10s | %-6s" % (ip, install_state, display_start, display_last, elapsed_time, files_counter)               
 
        # print "%s" % files