remove a lot of deprecated files ;
[monitor.git] / www / siteactions.php
1 <?php 
2
3 function get_category_link($category,$header) 
4
5         return "<a href='siteactions.php?category=$category'>$header</a>"; 
6 }
7 function rt_link ($ref, $id)
8 {
9         $slink = sprintf("<a href='https://rt.planet-lab.org/Ticket/Display.html".
10                         "?user=guest&pass=guest&id=%s'>%s #%s</a>", $id, $ref, $id);
11         return $slink;
12 }
13
14 function history_link($nodename)
15 {
16         return sprintf("<a href='siteactions.php?history=%s'>%s</a>", $nodename, $nodename);
17 }
18
19 class Event
20 {
21         function Event($event_record)
22         {
23                 $this->data = $event_record;
24         }
25         public function getTime()
26         {
27                 if ( array_key_exists('time', $this->data) )
28                 {
29                         $stime=strftime("%Y/%m/%d %H:%M", $this->data['time']);
30                         return $stime;
31                 } else {
32                         return "";
33                 }       
34         }
35         public function getField($field)
36         {
37                 if ( array_key_exists($field, $this->data ) )
38                 {
39                         # truncate the stage_ part.
40                         #event['stage'] = event['stage'][6:]
41                         if ( is_array($this->data[$field]) and $field != "info" ):
42                                 $ret = $this->data[$field][0];
43                         elseif ( is_array($this->data[$field]) and $field == "info" ):
44                                 $ar = $this->data[$field];
45                                 $ret = sprintf("%s -> %s", $ar[1] , $ar[2]);
46                         else:
47                                 $ret = $this->data[$field];
48                         endif;
49                 } else {
50                         $ret = "&nbsp;";
51                 }
52                 return $ret;
53
54         }
55         public function getRTTicket()
56         {
57                 if ( array_key_exists('ticket_id', $this->data) and 
58                      $this->data['ticket_id'] != "" )
59                 {
60                         $rt_ticket = $this->data['ticket_id'];
61                         $slink = rt_link("Monitor", $rt_ticket);
62
63                 } elseif ( array_key_exists('found_rt_ticket', $this->data) and 
64                                 $this->data['found_rt_ticket'] != "" ) {
65                         $rt_ticket = $this->data['found_rt_ticket'];
66                         $slink = rt_link("Support", $rt_ticket);
67                 } else {
68                         $slink = "No tickets";
69                 }
70                 return $slink;
71         }
72 }
73
74 include 'database.php';
75 $p = new Pickle();
76 $act_all = $p->load("act_all");
77 $plcdb_hn2lb = $p->load("plcdb_hn2lb");
78 $findbad = $p->load("findbadnodes");
79 $findbadnodes = array_keys($findbad['nodes']);
80
81 $sickdb = array();
82
83 $all_hosts = array_keys($act_all);
84 foreach ( $all_hosts as $nodename):
85         $diag_nodelist = $act_all[$nodename];
86         if ( array_key_exists($nodename, $plcdb_hn2lb) )
87                 $lb = $plcdb_hn2lb[$nodename];
88                 if ( ! array_key_exists($lb, $sickdb) )
89                         $sickdb[$lb] = array();
90                 $sickdb[$lb][$nodename] = $diag_nodelist;
91 endforeach;
92
93 if ( array_key_exists('history', $_GET) )
94 {
95         $history_nodename = $_GET['history'];
96         $all_loginbases = array($plcdb_hn2lb[$history_nodename]);
97         $get_history = TRUE;
98 } else {
99         $all_loginbases = array_keys($sickdb);
100         sort($all_loginbases);
101         $get_history = FALSE;
102 }
103
104 $display = array();
105
106 foreach ( $all_loginbases as $loginbase ):
107         $nodedict = $sickdb[$loginbase];
108         $hosts = array_keys($nodedict);
109         sort($hosts);
110         if ( $get_history ) {
111                 $nodename = $history_nodename;
112                 foreach ( $act_all[$nodename] as $event_record ):
113                         
114                         $event = new Event($event_record);
115                         $row = array();
116                         $row['loginbase'] = $loginbase;
117                         $row['hostname'] = history_link($nodename);
118                         $row['time'] = $event->getTime();
119                         $row['ticket_id'] = $event->getRTTicket();
120                         foreach ( array('category', 'action', 'stage', 'info') as $field )
121                         {
122                                 $row[$field] = $event->getField($field);
123                         }
124                         $display[] = $row;
125                 endforeach;
126         } elseif ( ! $get_history ) {
127                 foreach ( $hosts as $nodename ):
128                         if ( count($act_all[$nodename]) == 0 ) continue;
129                         
130                         $event = new Event($act_all[$nodename][0]);
131                         $row = array();
132                         $row['loginbase'] = $loginbase;
133                         $row['hostname'] = history_link($nodename);
134                         #$row['time'] = "<pre>" . print_r($act_all[$nodename][0], TRUE) .  "</pre>";
135                         $row['time'] = $event->getTime();
136                         $row['ticket_id'] = $event->getRTTicket();
137                         $row['plcstate'] = $event->data['plcnode']['boot_state'];
138                         $row['currentcategory'] = $findbad['nodes'][$nodename]['values']['category'] . 
139                                                                 "-" . $findbad['nodes'][$nodename]['values']['state'];
140                         foreach ( array('category', 'action', 'stage', 'info') as $field )
141                         {
142                                 $row[$field] = $event->getField($field);
143                         }
144
145                         $display[] = $row;
146                 endforeach;
147         }
148 endforeach;
149 ?>
150
151 <title>siteactions.php</title>
152 <html>
153 <body>
154
155
156 <table width=80% border=1>
157         <tr>
158                 <th>Count</th>
159                 <th><?= get_category_link("loginbase", "SiteID") ?></th>
160                 <th><?= get_category_link("hostname", "Hostname") ?></th>
161                 <?php if ( $get_history ): ?>
162                         <th><?= get_category_link("time", "Time") ?></th>
163                 <?php endif; ?>
164                 <th><?= get_category_link("ticket_id", "RT Ticket") ?></th>
165                 <th><?= get_category_link("category", "Last Category") ?></th>
166                 <?php if ( $get_history ): ?>
167                         <th><?= get_category_link("action", "Action") ?></th>
168                 <?php else: ?>
169                         <th><?= get_category_link("currentcategory", "Current Category") ?></th>
170                         <th><?= get_category_link("action", "Last Action") ?></th>
171                 <?php endif; ?>
172                 <th><?= get_category_link("stage", "Stage") ?></th>
173                 <th><?= get_category_link("info", "Info") ?></th>
174         </tr>
175 <?php $count = 0; ?>
176 <?php foreach ( $display as $row ): ?>
177         <tr>
178                 <td><?= $count ?></td>
179                 <td><?= $row['loginbase'] ?></td>
180                 <td><?= $row['hostname'] ?></td>
181                 <?php if ( $get_history ): ?>
182                         <td nowrap><?= $row['time'] ?></td>
183                 <?php endif; ?>
184                 <td nowrap><?= $row['ticket_id'] ?></td>
185                 <td><?= $row['category'] ?>/<?= $row['plcstate'] ?></td>
186                 <?php if ( ! $get_history ): ?>
187                         <td><?= $row['currentcategory'] ?></td>
188                 <?php endif; ?>
189                 <td><?= $row['action'] ?></td>
190                 <td><?= $row['stage'] ?></td>
191                 <td nowrap><?= $row['info'] ?></td>
192         </tr>
193 <?php $count += 1; ?>
194 <?php endforeach; ?>
195 </table>
196
197 </body>
198 </html>