source: trunk/cluster_status.php@ 6

Last change on this file since 6 was 6, checked in by gegorbet, 10 years ago

changes since 2013, mostly for airvata/thrift

File size: 7.5 KB
Line 
1<?php
2
3$us3bin = exec( "ls -d ~us3/bin" );
4include "$us3bin/listen-config.php";
5
6$xml = get_data();
7
8if ( $xml != "" )
9 parse( $xml );
10
11$data = array();
12
13local_status();
14
15foreach ( $data as $item )
16{
17 update( $item[ 'cluster' ], $item[ 'queued' ], $item[ 'status' ], $item[ 'running' ] );
18}
19
20exit();
21
22// Get the cluster status
23
24function get_data()
25{
26 global $self;
27 $url = "http://community.ucs.indiana.edu:19444/orps-service/XML/gateway/ultrascan";
28
29 try
30 {
31 $post = new HttpRequest( $url, HttpRequest::METH_GET );
32 $http = $post->send();
33 $xml = $post->getResponseBody();
34 }
35 catch ( HttpException $e )
36 {
37// write_log( "$self: Cluster Status not available" );
38 return "";
39 }
40
41 return $xml;
42}
43
44// Parse the xml
45
46function parse( $xml )
47{
48 global $data;
49
50 $data = array();
51
52 $x = new XML_Array( $xml );
53 $d = $x->ReturnArray();
54
55 if ( ! isset( $d[ 'summaries' ] ) ) exit(); // Bad input
56
57 foreach ( $d[ 'summaries' ] as $item )
58 {
59 $a = Array();
60
61
62 $a[ 'queued' ] = $item[ 'waitingJobs' ];
63 $a[ 'running' ] = $item[ 'runningJobs' ];
64
65 if ( $a[ 'queued' ] == "" || $a[ 'queued' ] < 0 ) $a[ 'queued' ] = 0;
66 if ( $a[ 'running' ] == "" || $a[ 'running' ] < 0 ) $a[ 'running' ] = 0;
67
68 $clusterParts = explode( ".", $item[ 'resourceId' ] );
69 $cluster = preg_replace( '/\d+$/', "", $clusterParts[ 0 ] );
70
71 if ( $cluster == 'uthscsa-bcf' ) $cluster = 'bcf';
72 if ( $cluster == 'uthscsa-alamo' ) $cluster = 'alamo';
73
74 $a[ 'cluster' ] = $cluster;
75
76 switch ( $item[ 'resourceStatus' ] )
77 {
78 case 'UP' :
79 $status = "up";
80 break;
81
82 case 'DOWN' :
83 $status = "down";
84 break;
85
86 case 'WARN' :
87 $status = "warn";
88 break;
89
90 case 'FAILED' :
91 default :
92 $status = "unknown";
93 break;
94 }
95
96 $a[ 'status' ] = $status;
97
98 $data[] = $a;
99 }
100}
101
102// Put it in the DB
103
104function update( $cluster, $queued, $status, $running )
105{
106 global $dbhost;
107 global $guser;
108 global $gpasswd;
109 global $gDB;
110 global $self;
111
112 $gfac_link = mysql_connect( $dbhost, $guser, $gpasswd );
113 $result = mysql_select_db( $gDB, $gfac_link );
114
115 if ( ! $result )
116 {
117 write_log( "$self: Could not connect to DB $gDB" );
118 echo "Could not connect to DB $gDB.\n";
119 exit();
120 }
121
122 $query = "SELECT * FROM cluster_status WHERE cluster='$cluster'";
123 $result = mysql_query( $query, $gfac_link );
124
125 if ( ! $result )
126 {
127 write_log( "$self: Query failed $query - " . mysql_error( $gfac_link ) );
128 echo "$self: Query failed $query - " . mysql_error( $gfac_link ) . "\n";
129 exit();
130 }
131
132 $rows = mysql_num_rows( $result );
133
134 if ( $rows == 0 ) // INSERT
135 {
136 $query = "INSERT INTO cluster_status SET " .
137 "cluster='$cluster', " .
138 "queued=$queued, " .
139 "running=$running, " .
140 "status='$status'";
141 }
142 else // UPDATE
143 {
144 $query = "UPDATE cluster_status SET " .
145 "queued=$queued, " .
146 "running=$running, " .
147 "status='$status' " .
148 "WHERE cluster='$cluster'";
149 }
150
151 $result = mysql_query( $query, $gfac_link );
152
153 if ( ! $result )
154 {
155 write_log( "$self: Query failed $query - " . mysql_error( $gfac_link ) );
156 echo "$self: Query failed $query - " . mysql_error( $gfac_link ) . "\n";
157 }
158}
159
160// Get local cluster status
161
162function local_status()
163{
164 global $self;
165 global $data;
166
167// $clusters = array( "alamo", "jacinto", "bcf" );
168 $clusters = array( "alamo", "jacinto" );
169// $clusters = array( "alamo" );
170 foreach ( $clusters as $clname )
171 {
172 $a = Array();
173 if ( $clname == "alamo" )
174 {
175 $qstat = `ssh $clname '/usr/bin/qstat -B 2>&1|tail -1'`;
176
177 $sparts = preg_split( '/\s+/', $qstat );
178 $que = $sparts[ 3 ];
179 $run = $sparts[ 4 ];
180 $sta = $sparts[ 10 ];
181 }
182 else
183 {
184 $qstat = `ssh $clname '/opt/torque/bin/qstat -B 2>&1|tail -1'`;
185
186 $sparts = preg_split( '/\s+/', $qstat );
187 $que = $sparts[ 3 ];
188 $run = $sparts[ 4 ];
189 $sta = $sparts[ 9 ];
190 }
191
192//echo "$self: cln que run sta $clname $que $run $sta \n";
193
194 if ( $sta == "Active" )
195 {
196 $sta = "up";
197 }
198 else
199 {
200 $sta = "down";
201 $que = "0";
202 $run = "0";
203 }
204
205 $a[ 'cluster' ] = $clname;
206 $a[ 'queued' ] = $que;
207 $a[ 'running' ] = $run;
208 $a[ 'status' ] = $sta;
209
210 $data[] = $a;
211 $a[ 'cluster' ] = $clname . "-local";
212 $data[] = $a;
213 }
214}
215
216class XML_Array
217{
218 var $_data = Array();
219 var $_name = Array();
220 var $_rep = Array();
221 var $_parser = 0;
222 var $_level = 0;
223 var $_index = 0;
224
225 function XML_Array( &$data )
226 {
227 $this->_parser = xml_parser_create();
228
229 xml_set_object ( $this->_parser, $this );
230 xml_parser_set_option ( $this->_parser, XML_OPTION_CASE_FOLDING, false );
231 xml_set_element_handler ( $this->_parser, "_startElement", "_endElement" );
232 xml_set_character_data_handler( $this->_parser, "_cdata" );
233
234 $this->_data = array();
235 $this->_level = 0;
236
237 if ( ! xml_parse( $this->_parser, $data, true ) )
238 return false;
239
240 xml_parser_free( $this->_parser );
241 }
242
243 function & ReturnArray()
244 {
245 return $this->_data[ 0 ];
246 }
247
248 function _startElement( $parser, $name, $attrs )
249 {
250 if ( $name == "resourceHealth" )
251 {
252## $name .= $this->_index;
253 $this->_index++;
254 }
255
256 if ( ! isset( $this->_rep[ $name ] ) ) $this->_rep[ $name ] = 0;
257
258 $this->_addElement( $name, $this->_data[ $this->_level ], $attrs );
259 $this->_name[ $this->_level ] = $name;
260 $this->_level++;
261 }
262
263 function _endElement( $parser, $name )
264 {
265 if ( isset( $this->_data[ $this->_level ] ) )
266 {
267 $this->_addElement( $this->_name[ $this->_level - 1 ],
268 $this->_data[ $this->_level - 1 ],
269 $this->_data[ $this->_level ]
270 );
271 }
272
273 unset( $this->_data[ $this->_level ] );
274 $this->_level--;
275 $this->_rep[ $name ]++;
276 }
277
278 function _cdata( $parser, $data )
279 {
280 if ( $this->_name[ $this->_level - 1 ] )
281 {
282 $this->_addElement( $this->_name[ $this->_level - 1 ],
283 $this->_data[ $this->_level - 1 ],
284 str_replace( array( "&gt;", "&lt;","&quot;", "&amp;" ),
285 array( ">" , "<" , '"' , "&" ),
286 $data
287 )
288 );
289 }
290 }
291
292 function _addElement( &$name, &$start, $add = array() )
293 {
294 if ( ( sizeof( $add ) == 0 && is_array( $add ) ) || ! $add )
295 {
296 if ( ! isset( $start[ $name ] ) ) $start[ $name ] = '';
297 $add = '';
298 }
299
300 $update = &$start[ $name ];
301
302 if ( is_array( $add) &&
303 is_array( $update ) ) $update += $add;
304 elseif ( is_array( $update ) ) return;
305 elseif ( is_array( $add ) ) $update = $add;
306 elseif ( $add ) $update .= $add;
307 }
308}
309?>
Note: See TracBrowser for help on using the repository browser.