| | 18 | /* |
| | 19 | * This script generate the file configuration escalations.cfg used by Nagios. |
| | 20 | * This script is shared to 6 parts: |
| | 21 | * - PART 1 : Collect Escalations for all Hosts |
| | 22 | * - PART 2 : Collect Escalations for all HostGroups |
| | 23 | * - PART 3 : Collect Escalations for all ServiceGroups |
| | 24 | * - PART 4 : Collect Escalations for all Services |
| | 25 | * - PART 5 : Generate the configuration with all datas collected |
| | 26 | * |
| | 27 | * PHP version 5 |
| | 28 | * |
| | 29 | * @package genEscalations.php |
| | 30 | * @author Damien Duponchelle dduponchelle@merethis.com |
| | 31 | * @version $Id: $ |
| | 32 | * @copyright (c) 2007-2008 Centreon |
| | 33 | * @license http://opensource.org/licenses/gpl-license.php GNU Public License |
| | 34 | */ |
| | 35 | |
| 76 | | if ($strTemp) $strDef .= print_line("contact_groups", $strTemp); |
| 77 | | if ($escalation["first_notification"] != NULL) $strDef .= print_line("first_notification", $escalation["first_notification"]); |
| 78 | | if ($escalation["last_notification"] != NULL) $strDef .= print_line("last_notification", $escalation["last_notification"]); |
| 79 | | if ($escalation["notification_interval"]!= NULL) $strDef .= print_line("notification_interval", $escalation["notification_interval"]); |
| 80 | | |
| 81 | | // Nagios 2 & 3 |
| 82 | | |
| 83 | | if ($oreon->user->get_version() >= 2) { |
| 84 | | $DBRESULT2 =& $pearDB->query("SELECT tp_name FROM timeperiod WHERE tp_id = '".$escalation["escalation_period"]."'"); |
| 85 | | if (PEAR::isError($DBRESULT2)) |
| 86 | | print "DB Error : ".$DBRESULT2->getDebugInfo()."<br />"; |
| 87 | | $tp =& $DBRESULT2->fetchRow(); |
| 88 | | if ($tp["tp_name"]) $strDef .= print_line("escalation_period", $tp["tp_name"]); |
| 89 | | if ($escalation["escalation_options1"]) $strDef .= print_line("escalation_options", $escalation["escalation_options1"]); |
| 90 | | $DBRESULT2->free(); |
| 91 | | } |
| | 113 | if (isset($strTemp) == true) $strDef .= print_line("contact_groups", $strTemp); |
| | 114 | if (isset($escalation["first_notification"]) == true) $strDef .= print_line("first_notification", $escalation["first_notification"]); |
| | 115 | if (isset($escalation["last_notification"]) == true) $strDef .= print_line("last_notification", $escalation["last_notification"]); |
| | 116 | if (isset($escalation["notification_interval"]) == true) $strDef .= print_line("notification_interval", $escalation["notification_interval"]); |
| | 117 | |
| | 118 | $DBRESULT2 =& $pearDB->query("SELECT tp_name FROM timeperiod WHERE tp_id = '".$escalation["escalation_period"]."'"); |
| | 119 | if (PEAR::isError($DBRESULT2)) { |
| | 120 | print "DB Error : ".$DBRESULT2->getDebugInfo()."<br />"; |
| | 121 | } |
| | 122 | |
| | 123 | $tp =& $DBRESULT2->fetchRow(); |
| | 124 | if (isset($tp["tp_name"]) == true) $strDef .= print_line("escalation_period", $tp["tp_name"]); |
| | 125 | if (isset($escalation["escalation_options1"]) == true) $strDef .= print_line("escalation_options", $escalation["escalation_options1"]); |
| | 126 | |
| | 127 | $DBRESULT2->free(); |
| | 181 | |
| | 182 | $cg = array(); |
| | 183 | $strTemp = NULL; |
| | 184 | |
| | 185 | $DBRESULT2 =& $pearDB->query("SELECT DISTINCT cg.cg_id, cg.cg_name FROM escalation_contactgroup_relation ecgr, contactgroup cg WHERE ecgr.escalation_esc_id = '".$escalation["esc_id"]."' AND ecgr.contactgroup_cg_id = cg.cg_id ORDER BY cg.cg_name"); |
| | 186 | if (PEAR::isError($DBRESULT2)) { |
| | 187 | print "DB Error : ".$DBRESULT2->getDebugInfo()."<br />"; |
| | 188 | } |
| | 189 | |
| | 190 | while($cg =& $DBRESULT2->fetchRow()) { |
| | 191 | $BP = false; |
| | 192 | array_key_exists($cg["cg_id"], $gbArr[1]) ? $BP = true : $BP = false; |
| | 193 | |
| | 194 | if($BP) { |
| | 195 | $strTemp != NULL ? $strTemp .= ", ".$cg["cg_name"] : $strTemp = $cg["cg_name"]; |
| | 196 | } |
| | 197 | } |
| | 198 | |
| | 199 | $DBRESULT2->free(); |
| | 200 | |
| | 201 | if (isset($strTemp) == true) $str .= print_line("contact_groups", $strTemp); |
| | 202 | if ($escalation["first_notification"] != NULL) $str .= print_line("first_notification", $escalation["first_notification"]); |
| | 203 | if ($escalation["last_notification"] != NULL) $str .= print_line("last_notification", $escalation["last_notification"]); |
| | 204 | if ($escalation["notification_interval"] != NULL) $str .= print_line("notification_interval", $escalation["notification_interval"]); |
| | 205 | |
| | 206 | $DBRESULT2 =& $pearDB->query("SELECT tp_name FROM timeperiod WHERE tp_id = '".$escalation["escalation_period"]."'"); |
| | 207 | if (PEAR::isError($DBRESULT2)) { |
| | 208 | print "DB Error : ".$DBRESULT2->getDebugInfo()."<br />"; |
| | 209 | } |
| | 210 | |
| | 211 | $tp =& $DBRESULT2->fetchRow(); |
| | 212 | if (isset($tp["tp_name"]) == true) $str .= print_line("escalation_period", $tp["tp_name"]); |
| | 213 | if ($escalation["escalation_options1"]) $str .= print_line("escalation_options", $escalation["escalation_options1"]); |
| | 214 | $str .= "}\n\n"; |
| | 215 | $i++; |
| | 216 | } |
| | 217 | } |
| | 218 | unset($escalation); |
| | 219 | $DBRESULT->free(); |
| | 220 | |
| | 221 | // PART 3 - Escalations for all ServiceGroups |
| | 222 | $DBRESULT =& $pearDB->query("SELECT DISTINCT esc.* FROM escalation_servicegroup_relation esgr, escalation esc WHERE esgr.escalation_esc_id = esc.esc_id ORDER BY esc.esc_name"); |
| | 223 | if (PEAR::isError($DBRESULT)) { |
| | 224 | print "DB Error : ".$DBRESULT->getDebugInfo()."<br />"; |
| | 225 | } |
| | 226 | |
| | 227 | $escalation = array(); |
| | 228 | $strTemp = NULL; |
| | 229 | while($escalation =& $DBRESULT->fetchRow()) { |
| | 230 | $BP = false; |
| | 231 | |
| | 232 | $DBRESULT2 =& $pearDB->query("SELECT DISTINCT sg.sg_id, sg.sg_name FROM escalation_servicegroup_relation esgr, servicegroup sg WHERE esgr.escalation_esc_id = '".$escalation["esc_id"]."' AND sg.sg_id = esgr.servicegroup_sg_id"); |
| | 233 | if (PEAR::isError($DBRESULT2)) { |
| | 234 | print "DB Error : ".$DBRESULT2->getDebugInfo()."<br />"; |
| | 235 | } |
| | 236 | |
| | 237 | $sg = array(); |
| | 238 | |
| | 239 | while ($sg =& $DBRESULT2->fetchRow()) { |
| | 240 | $BP = false; |
| | 241 | array_key_exists($sg["sg_id"], $gbArr[5]) ? $BP = true : NULL; |
| | 242 | |
| | 243 | if($BP) { |
| | 244 | $strTemp != NULL ? $strTemp .= ", ".$sg["sg_name"] : $strTemp = $sg["sg_name"]; |
| | 245 | } |
| | 246 | } |
| | 247 | |
| | 248 | $DBRESULT2->free(); |
| | 249 | |
| | 250 | if (isset($strTemp) == true) { |
| | 251 | $ret["comment"] ? ($str .= "# '".$escalation["esc_name"]."' service (group) escalation definition ".$i."\n") : NULL; |
| | 252 | |
| | 253 | if ($ret["comment"] && $escalation["esc_comment"]) { |
| | 254 | $comment = array(); |
| | 255 | $comment = explode("\n", $escalation["esc_comment"]); |
| | 256 | |
| | 257 | foreach ($comment as $cmt) { |
| | 258 | $str .= "# ".$cmt."\n"; |
| | 259 | } |
| | 260 | } |
| | 261 | |
| | 262 | $str .= "define serviceescalation{\n"; |
| | 263 | $str .= print_line("servicegroup_name", $strTemp); |
| | 264 | |
| 145 | | if ($strTemp) $str .= print_line("contact_groups", $strTemp); |
| 146 | | if ($escalation["first_notification"] != NULL) $str .= print_line("first_notification", $escalation["first_notification"]); |
| 147 | | if ($escalation["last_notification"] != NULL) $str .= print_line("last_notification", $escalation["last_notification"]); |
| 148 | | if ($escalation["notification_interval"] != NULL) $str .= print_line("notification_interval", $escalation["notification_interval"]); |
| 149 | | |
| 150 | | /* |
| 151 | | * Nagios 2 |
| 152 | | */ |
| 153 | | |
| 154 | | if ($oreon->user->get_version() >= 2) { |
| 155 | | $DBRESULT2 =& $pearDB->query("SELECT tp_name FROM timeperiod WHERE tp_id = '".$escalation["escalation_period"]."'"); |
| 156 | | if (PEAR::isError($DBRESULT2)) |
| 157 | | print "DB Error : ".$DBRESULT2->getDebugInfo()."<br />"; |
| 158 | | $tp =& $DBRESULT2->fetchRow(); |
| 159 | | if ($tp["tp_name"]) $str .= print_line("escalation_period", $tp["tp_name"]); |
| 160 | | if ($escalation["escalation_options1"]) $str .= print_line("escalation_options", $escalation["escalation_options1"]); |
| 161 | | } |
| | 282 | |
| | 283 | if (isset($strTemp) == true) $str .= print_line("contact_groups", $strTemp); |
| | 284 | if (isset($escalation["first_notification"]) == true) $str .= print_line("first_notification", $escalation["first_notification"]); |
| | 285 | if (isset($escalation["last_notification"]) == true) $str .= print_line("last_notification", $escalation["last_notification"]); |
| | 286 | if (isset($escalation["notification_interval"]) == true) $str .= print_line("notification_interval", $escalation["notification_interval"]); |
| | 287 | |
| | 288 | $DBRESULT2 =& $pearDB->query("SELECT tp_name FROM timeperiod WHERE tp_id = '".$escalation["escalation_period"]."'"); |
| | 289 | if (PEAR::isError($DBRESULT2)) { |
| | 290 | print "DB Error : ".$DBRESULT2->getDebugInfo()."<br />"; |
| | 291 | } |
| | 292 | |
| | 293 | $tp =& $DBRESULT2->fetchRow(); |
| | 294 | if (isset($tp["tp_name"]) == true) $str .= print_line("escalation_period", $tp["tp_name"]); |
| | 295 | if (isset($escalation["escalation_options2"]) == true) $str .= print_line("escalation_options", $escalation["escalation_options2"]); |
| | 296 | |
| 169 | | $DBRESULT =& $pearDB->query("SELECT DISTINCT esc.* FROM escalation_servicegroup_relation esgr, escalation esc WHERE esgr.escalation_esc_id = esc.esc_id ORDER BY esc.esc_name"); |
| 170 | | if (PEAR::isError($DBRESULT)) |
| 171 | | print "DB Error : ".$DBRESULT->getDebugInfo()."<br />"; |
| 172 | | $escalation = array(); |
| 173 | | while($escalation =& $DBRESULT->fetchRow()) { |
| 174 | | $BP = false; |
| 175 | | $DBRESULT2 =& $pearDB->query("SELECT DISTINCT sg.sg_id, sg.sg_name FROM escalation_servicegroup_relation esgr, servicegroup sg WHERE esgr.escalation_esc_id = '".$escalation["esc_id"]."' AND sg.sg_id = esgr.servicegroup_sg_id"); |
| 176 | | if (PEAR::isError($DBRESULT2)) |
| 177 | | print "DB Error : ".$DBRESULT2->getDebugInfo()."<br />"; |
| 178 | | $sg = array(); |
| 179 | | $strTemp = NULL; |
| 180 | | while ($sg =& $DBRESULT2->fetchRow()) { |
| 181 | | $BP = false; |
| 182 | | array_key_exists($sg["sg_id"], $gbArr[5]) ? $BP = true : NULL; |
| 183 | | |
| 184 | | if ($BP) |
| 185 | | $strTemp != NULL ? $strTemp .= ", ".$sg["sg_name"] : $strTemp = $sg["sg_name"]; |
| 186 | | } |
| 187 | | $DBRESULT2->free(); |
| 188 | | if ($strTemp) { |
| 189 | | $ret["comment"] ? ($str .= "# '".$escalation["esc_name"]."' service (group) escalation definition ".$i."\n") : NULL; |
| 190 | | if ($ret["comment"] && $escalation["esc_comment"]) { |
| 191 | | $comment = array(); |
| 192 | | $comment = explode("\n", $escalation["esc_comment"]); |
| 193 | | foreach ($comment as $cmt) |
| 194 | | $str .= "# ".$cmt."\n"; |
| 195 | | } |
| 196 | | $str .= "define serviceescalation{\n"; |
| 197 | | $str .= print_line("servicegroup_name", $strTemp); |
| 198 | | $cg = array(); |
| 199 | | $strTemp = NULL; |
| 200 | | $DBRESULT2 =& $pearDB->query("SELECT DISTINCT cg.cg_id, cg.cg_name FROM escalation_contactgroup_relation ecgr, contactgroup cg WHERE ecgr.escalation_esc_id = '".$escalation["esc_id"]."' AND ecgr.contactgroup_cg_id = cg.cg_id ORDER BY cg.cg_name"); |
| 201 | | if (PEAR::isError($DBRESULT2)) |
| 202 | | print "DB Error : ".$DBRESULT2->getDebugInfo()."<br />"; |
| 203 | | while($cg =& $DBRESULT2->fetchRow()) { |
| 204 | | $BP = false; |
| 205 | | array_key_exists($cg["cg_id"], $gbArr[1]) ? $BP = true : $BP = false; |
| 206 | | |
| 207 | | if ($BP) |
| 208 | | $strTemp != NULL ? $strTemp .= ", ".$cg["cg_name"] : $strTemp = $cg["cg_name"]; |
| 209 | | } |
| 210 | | $DBRESULT2->free(); |
| 211 | | if ($strTemp) $str .= print_line("contact_groups", $strTemp); |
| 212 | | if ($escalation["first_notification"] != NULL) $str .= print_line("first_notification", $escalation["first_notification"]); |
| 213 | | if ($escalation["last_notification"] != NULL) $str .= print_line("last_notification", $escalation["last_notification"]); |
| 214 | | if ($escalation["notification_interval"] != NULL) $str .= print_line("notification_interval", $escalation["notification_interval"]); |
| 215 | | // Nagios 2 & 3 |
| 216 | | if ($oreon->user->get_version() >= 2) { |
| 217 | | $DBRESULT2 =& $pearDB->query("SELECT tp_name FROM timeperiod WHERE tp_id = '".$escalation["escalation_period"]."'"); |
| 218 | | if (PEAR::isError($DBRESULT2)) |
| 219 | | print "DB Error : ".$DBRESULT2->getDebugInfo()."<br />"; |
| 220 | | $tp =& $DBRESULT2->fetchRow(); |
| 221 | | if ($tp["tp_name"]) $str .= print_line("escalation_period", $tp["tp_name"]); |
| 222 | | if ($escalation["escalation_options2"]) $str .= print_line("escalation_options", $escalation["escalation_options2"]); |
| 223 | | } |
| 224 | | $str .= "}\n\n"; |
| 225 | | $i++; |
| 226 | | } |
| 227 | | } |
| 228 | | unset($escalation); |
| 229 | | $DBRESULT->free(); |
| 230 | | |
| 231 | | $DBRESULT =& $pearDB->query("SELECT DISTINCT service.service_activate, service.service_description, esr.service_service_id FROM service, escalation_service_relation esr WHERE esr.service_service_id = service.service_id ORDER BY esr.service_service_id"); |
| 232 | | if (PEAR::isError($DBRESULT)) |
| 233 | | print "DB Error : ".$DBRESULT->getDebugInfo()."<br />"; |