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