<?php

//erreur possible
//temps de réponse de l'agent long ou impossible
//dernier export complet
//dernier export diff
//clef serveur, clef client
//monitoring

require_once("class/Client.class.php");
require_once("class/Supervision.class.php");

$supervision = new Supervision();
if ($supervision->configFile) {
    if ($supervision->connectBddOpac()) {
        $result = mysql_query('SELECT * FROM institution
                                LEFT JOIN institution_status ON institution.code=institution_status.institution
                                WHERE level=\'I\' ORDER BY code ASC');
        if (!$result) {
            die('Requete invalide : ' . mysql_error());
        }
        $sites_update_complete = array();
        $sites_update_partial = array();
        $sites_last_update_partial = array();
        $sites_last_update_complet = array();
        $j = 0;
        while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
           if($row['alias']==''){
             $sites[$row['code']] = $row['code'].'.opac3d.fr';
           }
           else{
             $sites[$row['code']] = $row['alias'];
           }
           $sites_update_complete[$row['code']] = $supervision->updateInDay($row['last_update_complete']);
           $sites_update_partial[$row['code']] =  $supervision->updateInDay($row['last_update_partial']);
           $sites_last_update_complet[$row['code']] =  $row['last_update_complete'];
           $sites_last_update_partial[$row['code']] =  $row['last_update_partial'];
           $j++;
        }
        $temp = file_get_contents($supervision->configFile['serveur']['url'].'config/clients.ini');
        $temp = parse_ini_string($temp, TRUE);
        $j = 0;
        $date_check = date("YmdHis");
        foreach($temp as $client => $val) {
            if($val['ip_agent']!='webservices.esezhame.fr') {
                if ($client!='besse' && $client!='salinlb' && $client!='testagate' && $client!='londres' && $client!='agate335' && $client!='colomiers' && $client!='55555' && $client!='cahors'){
                    if ($sites[$client]){
                        $ip_agent = $val['ip_agent'];
                        $url_config = 'http://'.$val['ip_agent'].'/webservices/checker/test_config.php?idclient='.$client;
                        $url_infosclass = 'http://'.$val['ip_agent'].'/webservices/client/client.php?action=infosclass';
                        $url_monitoring = 'http://'.$val['ip_agent'].'/webservices/monitoring/monitoring.php?client='.$client;
                        $erreur_agent = $supervision->check_url($url_config);
                        $erreur_config = $supervision->check_config($url_config);
                        $infos_monitoring = $supervision->check_monitoring($url_monitoring);
                        $class_name = "";
                        $class_version = "";
                        if ($erreur_agent != "1") {
                            $res = $supervision->check_infosclass($url_infosclass);
                            if (isset($res) && $res!="0"){
                                $info_tmp = explode("|",$res);
                                $class_name = $info_tmp[0];
                                $class_version = $info_tmp[1];
                            }
                        }
                        
                        if ($supervision->connectBddSupervision()) {
                            $clientSupervise = new Client();
                            $searchClient = $supervision->getClientByName($client);
                            if ($searchClient) {
                                $clientSupervise->setId($searchClient->getId());
                            }
                            $clientSupervise->setClient($client);
                            $clientSupervise->setUrl_opac3d($sites[$client]);
                            $clientSupervise->setUrl_opac3d($sites[$client]);
                            $clientSupervise->setIp_agent($ip_agent);
                            $clientSupervise->setUrl_testconfig($url_config);
                            $clientSupervise->setUrl_monitoring($url_monitoring);
                            $clientSupervise->setUrl_infosclass($url_infosclass);
                            $clientSupervise->setErreur_agent($erreur_agent);
                            $clientSupervise->setErreur_config($erreur_config);
                            $clientSupervise->setInfos_monitoring($infos_monitoring);
                            $clientSupervise->setLast_complet($sites_last_update_complet[$client]);
                            $clientSupervise->setLast_partiel($sites_last_update_partial[$client]);
                            $clientSupervise->setDate_check($date_check);
                            $clientSupervise->setClass_name($class_name);
                            $clientSupervise->setClass_version($class_version);
                            if ($clientSupervise->save()) {
                                echo 'Client '.$client.' supervisé<br />';
                            }
                            else {
                                echo 'Client '.$client.' : erreur de supervision<br />';
                            }
                        }
                        else {
                            die('Erreur de connexion à la base de supervision');
                        }
                    }
                }
            }
        }
    }
    else {
        die('Erreur de connexion à la base opac');
    }
}
else {
    die('Fichier de configuration de supervision non chargé');
}
