public static function getByHostTemplateAndDescription($hostTemplateName, $description) { // First get host template $template = NagiosHostTemplatePeer::getByName($hostTemplateName); if (!$template) { return false; } $c = new Criteria(); $c->add(NagiosServicePeer::HOST_TEMPLATE, $template->getId()); $c->add(NagiosServicePeer::DESCRIPTION, $description); $c->setIgnoreCase(true); $service = NagiosServicePeer::doSelectOne($c); if (!$service) { return false; } return $service; }
private function importHost($hostData) { $job = $this->getEngine()->getJob(); // check to see if we have a host by that name if (NagiosHostPeer::getByName($hostData['host_name'])) { $job->addNotice("Fruity Host Importer: Host " . $hostData['host_name'] . " already exists. Aborting it's import."); return true; } $newHost = new NagiosHost(); $newHost->setName($hostData['host_name']); // Check to see if we need the template if (!empty($hostData['use_template_id'])) { $name = $this->getHostTemplateNameById($hostData['use_template_id'], $this->dbConn); if (!$name) { $job->addNotice("Fruity Host Importer: Could not find template with id: " . $hostData['use_template_id'] . ". Aborting it's import."); return false; } else { // Okay, we got the name, does this template exist? $template = NagiosHostTemplatePeer::getByName($name); if (!$template) { $job->addNotice("Fruity Host Importer: Could not find a template in the system with the name of: " . $name . ". Aborting it's import."); return false; } else { // Create a new inheritance relationship $inheritance = new NagiosHostTemplateInheritance(); $inheritance->setNagiosHostTemplateRelatedByTargetTemplate($template); $inheritance->setNagiosHost($newHost); try { $inheritance->save(); } catch (Exception $e) { $job->addNotice("Fruity Host Importer: Cannot add inheritance from " . $template->getName() . " to " . $newHost->getName()); } } } } // Okay, start 'er up! foreach ($hostData as $key => $val) { unset($name); if ($key == "host_id" || $key == "use_template_id" || $key == "host_name") { continue; } if ($key == "parents") { // we're gonna do parents after this continue; } if ($key == "notification_options_down") { $key = "notification_on_down"; } if ($key == "notification_options_unreachable") { $key = "notification_on_unreachable"; } if ($key == "notification_options_recovery") { $key = "notification_on_recovery"; } if ($key == "notification_options_flapping") { $key = "notification_on_flapping"; } if ($key == "stalking_options_up") { $key = "stalking_on_up"; } if ($key == "stalking_options_down") { $key = "stalking_on_down"; } if ($key == "stalking_options_unreachable") { $key = "stalking_on_unreachable"; } if ($key == "max_check_attempts") { $key = "maximum_check_attempts"; } if ($key == "retry_check_interval") { $key = "retry_interval"; } if ($key == "check_command") { $name = $this->getCommandNameById($val); if ($name) { $command = NagiosCommandPeer::getByName($name); if ($command) { $newHost->setCheckCommand($command->getId()); } } continue; } if ($key == "check_period") { $name = $this->getTimeperiodNameById($val); if ($name) { $timeperiod = NagiosTimeperiodPeer::getByName($name); if ($timeperiod) { $newHost->setCheckPeriod($timeperiod->getId()); } } continue; } if ($key == "event_handler") { $name = $this->getCommandNameById($val); if ($name) { $command = NagiosCommandPeer::getByName($name); if ($command) { $newHost->setEventHandler($command->getId()); } } continue; } if ($key == "notification_period") { $name = $this->getTimeperiodNameById($val); if ($name) { $timeperiod = NagiosTimeperiodPeer::getByName($name); if ($timeperiod) { $newHost->setNotificationPeriod($timeperiod->getId()); } } continue; } try { $name = NagiosHostPeer::translateFieldName($key, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_PHPNAME); } catch (Exception $e) { $job->addNotice("Fruity Host Importer: Was unable to store unsupported value: " . $key); } if (!empty($name)) { $method = "set" . $name; $newHost->{$method}($val); } } $newHost->save(); $job->addNotice("FruityHostImporter: Imported new host: " . $newHost->getName()); $this->totalImported++; return true; }
private function importService($serviceData) { $job = $this->getEngine()->getJob(); $newService = new NagiosService(); // Check to see if host or host template exists first if (!empty($serviceData['host_id'])) { $hostName = $this->getHostNameById($serviceData['host_id']); if (!$hostName) { $job->addNotice("Fruity Service Importer: Failed to find host with id: " . $serviceData['host_id']); return true; } $host = NagiosHostPeer::getByName($hostName); if (!$host) { $job->addNotice("Fruity Service Importer: Failed to find host with name: " . $hostName); return true; } $newService->setHost($host->getId()); } else { if (!empty($serviceData['host_template_id'])) { $hostTemplateName = $this->getHostTemplateNameById($serviceData['host_template_id']); if (!$hostTemplateName) { $job->addNotice("Fruity Service Importer: Failed to find host template with id: " . $serviceData['host_template_id']); return true; } $hostTemplate = NagiosHostTemplatePeer::getByName($hostTemplateName); if (!$hostTemplate) { $job->addNotice("Fruity Service Importer: Failed to find host template with name: " . $hostTemplateName); return true; } $newService->setHostTemplate($hostTemplate->getId()); } else { if (!empty($serviceData['hostgroup_id'])) { $hostgroupName = $this->getHostGroupNameById($serviceData['hostgroup_id']); if (!$hostgroupName) { $job->addNotice("Fruity Service Importer: Failed to find host group with id: " . $serviceData['host_template_id']); return true; } $hostgroup = NagiosHostGroupPeer::getByName($hostgroupName); if (!$hostgroup) { $job->addNotice("Fruity Service Importer: Failed to find host group with name: " . $hostGroupName); return true; } $newService->setHostgroup($hostgroup->getId()); } } } // Check to see if we need the template if (!empty($serviceData['use_template_id'])) { $name = $this->getServiceTemplateNameById($serviceData['use_template_id'], $this->dbConn); if (!$name) { $job->addNotice("Fruity Service Importer: Could not find template with id: " . $serviceData['use_template_id'] . ". Aborting it's import."); return false; } else { // Okay, we got the name, does this template exist? $template = NagiosServiceTemplatePeer::getByName($name); if (!$template) { return false; } else { // Create a new inheritance relationship $inheritance = new NagiosServiceTemplateInheritance(); $inheritance->setNagiosServiceTemplateRelatedByTargetTemplate($template); $inheritance->setNagiosService($newService); try { $inheritance->save(); } catch (Exception $e) { $job->addNotice("Fruity Service Template Importer: Cannot add inheritance from " . $template->getName() . " to " . $newService->getName()); } } } } // Okay, start 'er up! foreach ($serviceData as $key => $val) { unset($name); if ($key == "service_template_id" || $key == "use_template_id" || $key == "template_name" || $key == "host_id" || $key == "hostgroup_id" || $key == "host_template_id" || $key == "service_id") { continue; } if ($key == "service_description") { $key = "description"; } if ($key == "notification_options_warning") { $key = "notification_on_warning"; } if ($key == "notification_options_unknown") { $key = "notification_on_unknown"; } if ($key == "notification_options_critical") { $key = "notification_on_critical"; } if ($key == "notification_options_recovery") { $key = "notification_on_recovery"; } if ($key == "notification_options_flapping") { $key = "notification_on_flapping"; } if ($key == "stalking_options_warning") { $key = "stalking_on_warning"; } if ($key == "stalking_options_unknown") { $key = "stalking_on_unknown"; } if ($key == "stalking_options_critical") { $key = "stalking_on_critical"; } if ($key == "stalking_options_ok") { $key = "stalking_on_ok"; } if ($key == "max_check_attempts") { $key = "maximum_check_attempts"; } if ($key == "retry_check_interval") { $key = "retry_interval"; } if ($key == "check_command") { $name = $this->getCommandNameById($val); if ($name) { $command = NagiosCommandPeer::getByName($name); if ($command) { $newService->setCheckCommand($command->getId()); } } continue; } if ($key == "check_period") { $name = $this->getTimeperiodNameById($val); if ($name) { $timeperiod = NagiosTimeperiodPeer::getByName($name); if ($timeperiod) { $newService->setCheckPeriod($timeperiod->getId()); } } continue; } if ($key == "event_handler") { $name = $this->getCommandNameById($val); if ($name) { $command = NagiosCommandPeer::getByName($name); if ($command) { $newService->setEventHandler($command->getId()); } } continue; } if ($key == "notification_period") { $name = $this->getTimeperiodNameById($val); if ($name) { $timeperiod = NagiosTimeperiodPeer::getByName($name); if ($timeperiod) { $newService->setNotificationPeriod($timeperiod->getId()); } } continue; } try { $name = NagiosServiceTemplatePeer::translateFieldName($key, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_PHPNAME); } catch (Exception $e) { $job->addNotice("Fruity Service Importer: Was unable to store unsupported value: " . $key); } if (!empty($name)) { $method = "set" . $name; $newService->{$method}($val); } } $job->addNotice("Fruity Service Importer: Saved service: " . $newService->getDescription() . " on " . $newService->getOwnerDescription()); $newService->save(); $this->totalImported++; return true; }
function addTemplateInheritance($name) { // First get the template by name $template = NagiosHostTemplatePeer::getByName($name); if (!$template) { return false; } // Check to see if inheritance already exists $id = $this->getId(); if (!empty($id)) { $c = new Criteria(); $c->add(NagiosHostTemplateInheritancePeer::SOURCE_TEMPLATE, $this->getId()); $c->add(NagiosHostTemplateInheritancePeer::TARGET_TEMPLATE, $template->getId()); $relationship = NagiosHostTemplateInheritancePeer::doSelectOne($c); if ($relationship) { return false; } } // Okay, create new one $relationship = new NagiosHostTemplateInheritance(); $relationship->setNagiosHostTemplateRelatedBySourceTemplate($this); $relationship->setNagiosHostTemplateRelatedByTargetTemplate($template); $relationship->save(); return true; }
public function valid() { $values = $this->getSegment()->getValues(); $job = $this->getEngine()->getJob(); if (isset($values['use'])) { // We need to use a template $job->addNotice("This Host uses a template: " . $values['use'][0]['value']); $template = NagiosHostTemplatePeer::getByName($values['use'][0]['value']); if (empty($template)) { if (!isset($values['name'][0]['value'])) { $job->addNotice("That template is not found yet. Setting this host (" . $values['host_name'][0]['value'] . ") as queued."); } else { $job->addNotice("That template is not found yet. Setting this host template (" . $values['name'][0]['value'] . ") as queued."); } return false; } } // Check time period existence if (isset($values['check_period'])) { $c = new Criteria(); $c->add(NagiosTimeperiodPeer::NAME, $values['check_period'][0]['value']); $timePeriod = NagiosTimeperiodPeer::doSelectOne($c); if (empty($timePeriod)) { $job->addNotice("The time period specified by " . $values['check_period'][0]['value'] . " was not found."); return false; } $timePeriod->clearAllReferences(true); } if (isset($values['notification_period'])) { $c = new Criteria(); $c->add(NagiosTimeperiodPeer::NAME, $values['notification_period'][0]['value']); $timePeriod = NagiosTimeperiodPeer::doSelectOne($c); if (empty($timePeriod)) { $job->addNotice("The time period specified by " . $values['notification_period'][0]['value'] . " was not found."); return false; } $timePeriod->clearAllReferences(true); } // Check command existence if (isset($values['check_command'])) { $params = explode("!", $values['check_command'][0]['value']); $c = new Criteria(); $c->add(NagiosCommandPeer::NAME, $params[0]); $command = NagiosCommandPeer::doSelectOne($c); if (empty($command)) { $job->addNotice("The command specified by " . $params[0] . " was not found."); return false; } $command->clearAllReferences(true); } if (isset($values['event_handler'])) { $c = new Criteria(); $c->add(NagiosCommandPeer::NAME, $values['event_handler'][0]['value']); $command = NagiosCommandPeer::doSelectOne($c); if (empty($command)) { $job->addNotice("The command specified by " . $values['event_handler'][0]['value'] . " was not found."); return false; } $command->clearAllReferences(true); } // Check contact groups if (isset($values['contact_groups'])) { foreach ($values['contact_groups'] as $contactGroupValues) { $c = new Criteria(); $c->add(NagiosContactGroupPeer::NAME, $contactGroupValues['value']); $contactgroup = NagiosContactGroupPeer::doSelectOne($c); if (empty($contactgroup)) { $job->addNotice("The contact group specified by " . $contactGroupValues['value'] . " was not found."); return false; } $contactgroup->clearAllReferences(); } } if (isset($values['contacts'])) { foreach ($values['contacts'] as $contactValues) { $c = new Criteria(); $c->add(NagiosContactPeer::NAME, $contactValues['value']); $contactgroup = NagiosContactPeer::doSelectOne($c); if (empty($contactgroup)) { $job->addNotice("The contact specified by " . $contactValues['value'] . " was not found."); return false; } $contactgroup->clearAllReferences(); } } // Check host groups if (isset($values['hostgroups'])) { foreach ($values['hostgroups'] as $hostGroupValues) { $c = new Criteria(); $c->add(NagiosHostgroupPeer::NAME, $hostGroupValues['value']); $hostgroup = NagiosHostgroupPeer::doSelectOne($c); if (empty($hostgroup)) { $job->addNotice("The host group specified by " . $hostGroupValues['value'] . " was not found."); return false; } $hostgroup->clearAllReferences(); } } // Check parents if (isset($values['parents'])) { foreach ($values['parents'] as $parentValues) { $c = new Criteria(); $c->add(NagiosHostPeer::NAME, $parentValues['value']); $host = NagiosHostPeer::doSelectOne($c); if (empty($host)) { $job->addNotice("The host specified by " . $parentValues['value'] . " was not found."); return false; } $host->clearAllReferences(); } } return true; }
public function import() { $engine = $this->getEngine(); $job = $engine->getJob(); $job->addNotice("FruityDependencyImporter beginning to import Dependency Configuration."); foreach ($this->dbConn->query("SELECT * FROM nagios_dependencies", PDO::FETCH_ASSOC) as $dependency) { $newDependency = new NagiosDependency(); if (!empty($dependency['service_id'])) { // This is a service dependency $lilacService = $this->getLilacServiceById($dependency['service_id']); if (!$lilacService) { $job->addNotice("Fruity Dependency Importer: Failed to get Lilac service with an id matching: " . $dependency['service_id']); return true; } $newDependency->setService($lilacService->getId()); // Create Target $targetLilacService = $this->getLilacServiceById($dependency['target_service_id']); if (!$targetLilacService) { $job->addNotice("Fruity Dependency Importer: Failed to get Lilac service with an id matching: " . $dependency['target_service_id']); return true; } $target = new NagiosDependencyTarget(); $target->setNagiosDependency($newDependency); $target->setTargetService($targetLilacService->getId()); $target->save(); } else { if (!empty($dependency['host_id'])) { // This is a host dependency $hostName = $this->getHostNameById($dependency['host_id']); $host = NagiosHostPeer::getByName($hostName); if (!$host) { $job->addNotice("Fruity Dependency Importer: Failed to get Lilac host with an name matching: " . $hostName); return true; } $newDependency->setHost($host->getId()); // Create Target $hostName = $this->getHostNameById($dependency['target_host_id']); $host = NagiosHostPeer::getByName($hostName); if (!$host) { $job->addNotice("Fruity Dependency Importer: Failed to get Lilac host with an name matching: " . $hostName); return true; } $target = new NagiosDependencyTarget(); $target->setNagiosDependency($newDependency); $target->setTargetHost($host->getId()); $target->save(); } else { if (!empty($dependency['service_template_id'])) { // This is a service template dependency $templateName = $this->getServiceTemplateNameById($dependency['service_template_id']); $template = NagiosServiceTemplatePeer::getByName($templateName); if (!$template) { $job->addNotice("Fruity Dependency Importer: Failed to get Lilac service template with name matching: " . $templateName); return true; } $newDependency->setServiceTemplate($template->getId()); // Create Target $targetLilacService = $this->getLilacServiceById($dependency['target_service_id']); if (!$targetLilacService) { $job->addNotice("Fruity Dependency Importer: Failed to get Lilac service with an id matching: " . $dependency['target_service_id']); return true; } $target = new NagiosDependencyTarget(); $target->setNagiosDependency($newDependency); $target->setTargetService($targetLilacService->getId()); $target->save(); } else { if (!empty($dependency['host_template_id'])) { // This is for a host template dependency $templateName = $this->getHostTemplateNameById($dependency['host_template_id']); $template = NagiosHostTemplatePeer::getByName($templateName); if (!$template) { $job->addNotice("Fruity Dependency Importer: Failed to get Lilac host template with name matching: " . $templateName); return true; } $newDependency->setHostTemplate($template->getId()); $hostName = $this->getHostNameById($dependency['target_host_id']); $host = NagiosHostPeer::getByName($hostName); if (!$host) { $job->addNotice("Fruity Dependency Importer: Failed to get Lilac host with an name matching: " . $hostName); return true; } $target = new NagiosDependencyTarget(); $target->setNagiosDependency($newDependency); $target->setTargetHost($host->getId()); $target->save(); } } } } foreach ($dependency as $key => $val) { unset($name); if ($key == "dependency_id" || $key == "host_id" || $key == "host_template_id" || $key == "service_template_id" || $key == "service_id" || $key == "target_service_id" || $key == "target_host_id") { continue; } if ($key == "command_name") { $key = "name"; } if ($key == "command_line") { $key = "line"; } if ($key == "command_desc") { $key = "description"; } try { $name = NagiosDependencyPeer::translateFieldName($key, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_PHPNAME); } catch (Exception $e) { $job->addNotice("Fruity Dependency Importer: Was unable to store unsupported value: " . $key); } if (!empty($name)) { $method = "set" . $name; $newDependency->{$method}($val); } } $newDependency->save(); $newDependency->setName("Imported Dependency #" . $newDependency->getId()); $newDependency->save(); $job->addNotice("FruityDependencyImporter imported dependency with id: " . $newDependency->getId()); } $job->addNotice("FruityDependencyImporter finished importing Dependency Configuration."); }
public function import() { $engine = $this->getEngine(); $job = $engine->getJob(); $job->addNotice("FruityEscalationImporter beginning to import Dependency Configuration."); foreach ($this->dbConn->query("SELECT * FROM nagios_escalations", PDO::FETCH_ASSOC) as $escalation) { $newEscalation = new NagiosEscalation(); if (!empty($escalation['service_id'])) { // This is a service escalation $lilacService = $this->getLilacServiceById($escalation['service_id']); if (!$lilacService) { $job->addNotice("Fruity Escalation Importer: Failed to get Lilac service with an id matching: " . $escalation['service_id']); return true; } $newEscalation->setService($lilacService->getId()); } else { if (!empty($escalation['host_id'])) { // This is a host escalation $hostName = $this->getHostNameById($escalation['host_id']); $host = NagiosHostPeer::getByName($hostName); if (!$host) { $job->addNotice("Fruity Escalation Importer: Failed to get Lilac host with an name matching: " . $hostName); return true; } $newEscalation->setHost($host->getId()); } else { if (!empty($escalation['service_template_id'])) { // This is a service template escalation $templateName = $this->getServiceTemplateNameById($escalation['service_template_id']); $template = NagiosServiceTemplatePeer::getByName($templateName); if (!$template) { $job->addNotice("Fruity Escalation Importer: Failed to get Lilac service template with name matching: " . $templateName); return true; } $newEscalation->setServiceTemplate($template->getId()); } else { if (!empty($escalation['host_template_id'])) { // This is for a host template escalation $templateName = $this->getHostTemplateNameById($escalation['host_template_id']); $template = NagiosHostTemplatePeer::getByName($templateName); if (!$template) { $job->addNotice("Fruity Escalation Importer: Failed to get Lilac host template with name matching: " . $templateName); return true; } $newEscalation->setHostTemplate($template->getId()); } } } } foreach ($escalation as $key => $val) { unset($name); if ($key == "escalation_id" || $key == "host_id" || $key == "host_template_id" || $key == "service_template_id" || $key == "service_id") { continue; } if ($key == "escalation_description") { $key = "description"; } if ($key == "escalation_period") { $escalationName = $this->getTimeperiodNameById($id); if ($escalationName) { $newEscalation->setEscalationPeriodByName($escalationName); } continue; } try { $name = NagiosEscalationPeer::translateFieldName($key, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_PHPNAME); } catch (Exception $e) { $job->addNotice("Fruity Escalation Importer: Was unable to store unsupported value: " . $key); } if (!empty($name)) { $method = "set" . $name; $newEscalation->{$method}($val); } } $newEscalation->save(); // Handle escalation contact groups. foreach ($this->dbConn->query("SELECT * FROM nagios_escalation_contactgroups WHERE escalation_id = " . $escalation['escalation_id'], PDO::FETCH_ASSOC) as $contactgroup) { $contactgroupName = $this->getContactGroupNameById($contactgroup['contactgroup_id']); if ($contactgroupName) { $lilacContactGroup = NagiosContactGroupPeer::getByName($contactgroupName); if ($lilacContactGroup) { $newContactGroup = new NagiosEscalationContactgroup(); $newContactGroup->setEscalation($newEscalation->getId()); $newContactGroup->setContactgroup($lilacContactGroup->getId()); $newContactGroup->save(); } } } } $job->addNotice("FruityEscalationImporter finished importing Escalation Configuration."); }