#!/usr/bin/php time = time(); $dump->options = $opt; $dump->services_matrix = get_services_matrix(); $dump->session = $_SESSION; $dump->view_all_services = view_all_services(); $dump->service_status = array(); $dump->license_limits = check_license_limits(); $dump->is_valid_module = is_valid_module(); $dump->unable_to_locate_contact = FALSE; $dump->logged_user = get_logged_user(); $logged_contact = get_contact_by_name($dump->logged_user); if ($logged_contact === FALSE) { $dump->unable_to_locate_contact = TRUE; } else { $services_matrix_memcache_id = sprintf( "contact_%s_services_matrix", $logged_contact->get_contact_id() ); $dump->services_matrix_on_cache = OPCache::get( $services_matrix_memcache_id, TRUE ); } $ret = do_query( "select * from service_status", DBOPMON, OPDB_FETCH_OBJ ); while(!$ret->EOF) { $dump->service_status[] = $ret->fields; $ret->MoveNext(); } $ret->Close(); $r = @file_put_contents($fpath, serialize($dump)); if ($r === FALSE) { return "-enoperm"; } return $fpath; } function get_last_catalog_status($options) { if (isset($options['s'])) { $query = "SELECT a.scat_name,b.scat_type_name ". "FROM scats a, opcfg.scat_types b ". "WHERE a.scat_name='%s' ". "AND a.type = b.scat_type_id"; $query = sprintf($query, $options['s']); } else { $query = "SELECT * ". "FROM scats a, opcfg.scat_types b ". "WHERE a.id = '%d'". "AND a.type = b.scat_type_id"; $query = sprintf($query, $options['i']); } $sqlh = do_query($query, DBOPMON, OPDB_FETCH_OBJ); if ($sqlh->RecordCount() === 0) { print "Este catalogo nao existe. Possivelmente foi removido.\n"; $sqlh->Close(); return UNKNOWN; } $hostname = changeCharCode($sqlh->fields->scat_type_name); $servicedesc = changeCharCode($sqlh->fields->scat_name); $sqlh->Close(); $query = "SELECT current_state, plugin_output ". "FROM service_status ". "WHERE host_name='%s' ". "AND service_description='%s'"; $query = sprintf( $query, mysql_real_escape_string($hostname), mysql_real_escape_string($servicedesc) ); $sqlh = do_query($query, DBOPMON, OPDB_FETCH_OBJ); if ($sqlh->RecordCount() == 0) { print "O OpMon ainda esta carregando seus objetos, por favor aguarde proxima checagem\n"; $sqlh->Close(); return OK; } print "{$sqlh->fields->plugin_output}\n"; $r = $sqlh->fields->current_state; $sqlh->Close(); return $r; } function main() { $options = getopt("s:i:d:h"); if (isset($options['h'])) usage(); if (!isset($options['s']) && !isset($options['i'])) usage(); if (wait_for_load() === FALSE) { $e = get_last_catalog_status($options); exit($e); } $myscat = false; if (isset($options['s'])) { $scats = get_scats(); foreach($scats as $s) { if (utf8_decode($s->name) == $options['s']) { $myscat = $s; break; } } } elseif(isset($options['i'])) { if (!is_numeric($options['i'])){ print "ID invalido\n"; exit(UNKNOWN); } $myscat = get_scat_by_id($options['i']); } $myscat->set_sched_as_ok(true); //var_dump($myscat); exit; if ($myscat == false) { $fname = (isset($options['i'])) ? "id" : "scat_name"; $fvalue = (isset($options['i'])) ? $options['i'] : mysql_real_escape_string($options['s']); $query = "select * from scats where {$fname} = '{$fvalue}'"; $result = do_query($query,DBOPMON,OPDB_FETCH_OBJ); if ($result->RecordCount() != 1) { print "Impossivel encontrar o servico ({$fvalue})\n"; } else { $fvalue = dump_me($options); print "Sem permissao de acesso a um ou mais componentes do catalogo ({$fvalue})\n"; } $result->Close(); exit(UNKNOWN); } $exit_code = $myscat->get_status(); if ($exit_code == OK) { print "Servico do catalogo em funcionamento normal.\n"; exit(OK); } $plugin_output = "%d elementos do servico com problema: %s\n"; $prbl_elems = 0; $problems = array(); foreach($myscat->levels as $level => $elements) { foreach($elements as $e) { if ($e->service_id === null) { // scat inside scat $s = get_scat_by_id($e->host_id); if ($s === false) { $problems[] = "Unable to locate scat by id $e->host_id"; } } else { $s = get_service_by_id($e->host_id,$e->service_id); if ($s === false) { $problems[] = "Unable to locate service({$e->host_id},{$e->service_id})"; } } $status = $s->get_status(); if ($e->service_id === null) { // scat inside scat $current_state = $status; $desc = "{$s->name}"; } else { $current_state = $status->current_state; $desc = "{$status->host_name}({$status->service_description})"; } /* * Alteracao Interop */ if($options['d'] == 1) { if( $current_state != OK and $status->scheduled_downtime_depth == 1 ) { $current_state = OK; $desc .= " Com Parada Agendada"; $exit_code = 0; } }elseif($options['d'] == 2) { if( $current_state != OK and $status->scheduled_downtime_depth == 1 ) { $current_state = WARNING; $desc .= " Com Parada Agendada"; $exit_code = 1; } } /* * Fim alteração Interop */ if ($current_state != OK ) { $prbl_elems++; switch($current_state) { case WARNING: $desc .= " WARNING"; break; case CRITICAL: $desc .= " CRITICAL"; break; default: $desc .= " UNKNOWN"; break; } $problems[] = $desc; } } } print sprintf($plugin_output, $prbl_elems, implode(",",$problems)); exit($exit_code); } function usage() { print "Usage: ./check_scat_status.php [-s ] [-i ] opcional: [-d [0|1]]\n"; print "\t -d 1 - Forçar Paradas Agendadas como OK \n"; print "\t -d 2 - Forçar Paradas Agendadas como WARNING \n"; print "\t -d Nao definido, comportamento padrão do sistema. \n"; exit(UNKNOWN); } main(); ?>