public function export() { // Grab our export job $engine = $this->getEngine(); $job = $engine->getJob(); $job->addNotice("NagiosMainExporter attempting to export main configuration."); // Grab our main config $mainConfig = NagiosMainConfigurationPeer::doSelectOne(new Criteria()); if (!$mainConfig) { $job->addError("Unable to get Main Configuration object. Your Lilac database is corrupt."); return false; } $finalArray = array(); $commandLookupArray = array('global_host_event_handler', 'global_service_event_handler', 'ocsp_command', 'ochp_command', 'host_perfdata_command', 'service_perfdata_command', 'host_perfdata_file_processing_command', 'service_perfdata_command', 'service_perfdata_file_processing_command'); $values = $mainConfig->toArray(BasePeer::TYPE_FIELDNAME); foreach ($values as $key => $value) { if ($key == 'id' || $key == 'config_dir') { continue; } if ($value === null) { continue; } if ($value === false) { $value = '0'; } if (in_array($key, $commandLookupArray)) { $command = NagiosCommandPeer::retrieveByPK($value); if (!$command) { $job->addError("Unable to find command with id:" . $value . " for " . $key); return false; } else { $value = $command->getName(); } } $finalArray[$key] = $value; } $fp = $this->getOutputFile(); fputs($fp, "# Written by NagiosMainExporter from " . LILAC_NAME . " " . LILAC_VERSION . " on " . date("F j, Y, g:i a") . "\n\n"); foreach ($finalArray as $key => $val) { fputs($fp, $key . "=" . $val . "\n"); } // Get list of broker modules $modules = NagiosBrokerModulePeer::doSelect(new Criteria()); foreach ($modules as $mod) { fputs($fp, "broker_module=" . $mod->getLine() . "\n"); } if (!empty($this->configDir)) { fputs($fp, "resource_file=" . $this->configDir . "/resource.cfg\n"); } else { fputs($fp, "resource_file=" . $mainConfig->getConfigDir() . "/resource.cfg\n"); } if (!empty($this->configDir)) { fputs($fp, "cfg_dir=" . $this->configDir . "/objects\n"); } else { fputs($fp, "cfg_dir=" . $mainConfig->getConfigDir() . "/objects\n"); } $job->addNotice("NagiosMainExporter complete."); return true; }
public function export() { // Grab our export job $engine = $this->getEngine(); $job = $engine->getJob(); $job->addNotice("NagiosCgiExporter attempting to export cgi configuration."); // Grab our cgi config $cgiConfig = NagiosCgiConfigurationPeer::doSelectOne(new Criteria()); if (!$cgiConfig) { $job->addError("Unable to get CGI Configuration object. Your Lilac database is corrupt."); return false; } $finalArray = array(); $values = $cgiConfig->toArray(BasePeer::TYPE_FIELDNAME); foreach ($values as $key => $value) { if ($key == 'id') { continue; } if ($value === null) { continue; } if ($value === false) { $value = '0'; } $finalArray[$key] = $value; } // get our main config $mainConfig = NagiosMainConfigurationPeer::doSelectOne(new Criteria()); $configdir = $mainConfig->getConfigDir(); $finalArray['main_config_file'] = $configdir . "/nagios.cfg"; $fp = $this->getOutputFile(); fputs($fp, "# Written by NagiosCgiExporter from " . LILAC_NAME . " " . LILAC_VERSION . " on " . date("F j, Y, g:i a") . "\n\n"); foreach ($finalArray as $key => $val) { fputs($fp, $key . "=" . $val . "\n"); } /* Added by Romain Dessort (Evolix) on 08/02/2011: * * nagios_check_command directive is required by Nagios, but * Lilac does not allow to change this parameter in the web * interface. I set it here. */ fputs($fp, "nagios_check_command=/usr/lib/nagios/plugins/check_nagios /var/cache/nagios3/status.dat 5 '/usr/sbin/nagios3'\n"); $job->addNotice("NagiosCgiExporter complete."); return true; }
public function export() { $job = $this->getJob(); $job->addNotice("NagiosExportEngine beginning export..."); $config = $this->getConfig(); // Main Configuration $fp = @fopen($this->exportDir . "/nagios.cfg", "w"); if (!$fp) { $job->addError("Unable to open " . $this->exportDir . "/nagios.cfg for writing."); return false; } $exporter = new NagiosMainExporter($this, $fp); $exporter->setConfigDir($this->exportDir); $exporter->init(); if (!$exporter->valid()) { $this->addQueuedExporter($exporter); $job->addNotice("NagiosImportEngine queueing up Main exporter until dependencies are valid."); } else { if (!$exporter->export()) { $job->addError("Main Exporter failed export. Bailing out of job..."); return false; } } // CGI Configuration $fp = @fopen($this->exportDir . "/cgi.cfg", "w"); if (!$fp) { $job->addError("Unable to open " . $this->exportDir . "/cgi.cfg for writing."); return false; } $exporter = new NagiosCgiExporter($this, $fp); $exporter->init(); if (!$exporter->valid()) { $this->addQueuedExporter($exporter); $job->addNotice("NagiosImportEngine queueing up CGI exporter until dependencies are valid."); } else { if (!$exporter->export()) { $job->addError("CGI Exporter failed export. Bailing out of job..."); return false; } } // Resource Configuration $fp = @fopen($this->exportDir . "/resource.cfg", "w"); if (!$fp) { $job->addError("Unable to open " . $this->exportDir . "/resource.cfg for writing."); return false; } $exporter = new NagiosResourceExporter($this, $fp); $exporter->init(); if (!$exporter->valid()) { $this->addQueuedExporter($exporter); $job->addNotice("NagiosImportEngine queueing up Resource exporter until dependencies are valid."); } else { if (!$exporter->export()) { $job->addError("Resource Exporter failed export. Bailing out of job..."); return false; } } // Command Configuration $fp = @fopen($this->exportDir . "/objects/commands.cfg", "w"); if (!$fp) { $job->addError("Unable to open " . $this->exportDir . "/objects/commands.cfg for writing."); return false; } $exporter = new NagiosCommandExporter($this, $fp); $exporter->init(); if (!$exporter->valid()) { $this->addQueuedExporter($exporter); $job->addNotice("NagiosImportEngine queueing up Command exporter until dependencies are valid."); } else { if (!$exporter->export()) { $job->addError("Command Exporter failed export. Bailing out of job..."); return false; } } // Timeperiod Configuration $fp = @fopen($this->exportDir . "/objects/timeperiods.cfg", "w"); if (!$fp) { $job->addError("Unable to open " . $this->exportDir . "/objects/timeperiods.cfg for writing."); return false; } $exporter = new NagiosTimePeriodExporter($this, $fp); $exporter->init(); if (!$exporter->valid()) { $this->addQueuedExporter($exporter); $job->addNotice("NagiosImportEngine queueing up Timeperiod exporter until dependencies are valid."); } else { if (!$exporter->export()) { $job->addError("Time Period Exporter failed export. Bailing out of job..."); return false; } } // Contact Configuration $fp = @fopen($this->exportDir . "/objects/contacts.cfg", "w"); if (!$fp) { $job->addError("Unable to open " . $this->exportDir . "/objects/contacts.cfg for writing."); return false; } $exporter = new NagiosContactExporter($this, $fp); $exporter->init(); if (!$exporter->valid()) { $this->addQueuedExporter($exporter); $job->addNotice("NagiosImportEngine queueing up Contact exporter until dependencies are valid."); } else { if (!$exporter->export()) { $job->addError("Contact Exporter failed export. Bailing out of job..."); return false; } } // Contact Group Configuration $fp = @fopen($this->exportDir . "/objects/contactgroups.cfg", "w"); if (!$fp) { $job->addError("Unable to open " . $this->exportDir . "/objects/contactgroups.cfg for writing."); return false; } $exporter = new NagiosContactGroupExporter($this, $fp); $exporter->init(); if (!$exporter->valid()) { $this->addQueuedExporter($exporter); $job->addNotice("NagiosImportEngine queueing up Contact Group exporter until dependencies are valid."); } else { if (!$exporter->export()) { $job->addError("Contact Group Exporter failed export. Bailing out of job..."); return false; } } // Host Group Configuration $fp = @fopen($this->exportDir . "/objects/hostgroups.cfg", "w"); if (!$fp) { $job->addError("Unable to open " . $this->exportDir . "/objects/hostgroups.cfg for writing."); return false; } $exporter = new NagiosHostGroupExporter($this, $fp); $exporter->init(); if (!$exporter->valid()) { $this->addQueuedExporter($exporter); $job->addNotice("NagiosImportEngine queueing up Host Group exporter until dependencies are valid."); } else { if (!$exporter->export()) { $job->addError("Host Group Exporter failed export. Bailing out of job..."); return false; } } // Service Group Configuration $fp = @fopen($this->exportDir . "/objects/servicegroups.cfg", "w"); if (!$fp) { $job->addError("Unable to open " . $this->exportDir . "/objects/servicegroups.cfg for writing."); return false; } $exporter = new NagiosServiceGroupExporter($this, $fp); $exporter->init(); if (!$exporter->valid()) { $this->addQueuedExporter($exporter); $job->addNotice("NagiosImportEngine queueing up Service Group exporter until dependencies are valid."); } else { if (!$exporter->export()) { $job->addError("Service Group Exporter failed export. Bailing out of job..."); return false; } } // Host Configuration $fp = @fopen($this->exportDir . "/objects/hosts.cfg", "w"); if (!$fp) { $job->addError("Unable to open " . $this->exportDir . "/objects/hosts.cfg for writing."); return false; } $exporter = new NagiosHostExporter($this, $fp); $exporter->init(); if (!$exporter->valid()) { $this->addQueuedExporter($exporter); $job->addNotice("NagiosImportEngine queueing up Host exporter until dependencies are valid."); } else { if (!$exporter->export()) { $job->addError("Host Exporter failed export. Bailing out of job..."); return false; } } // Service Configuration $fp = @fopen($this->exportDir . "/objects/services.cfg", "w"); if (!$fp) { $job->addError("Unable to open " . $this->exportDir . "/objects/services.cfg for writing."); return false; } $exporter = new NagiosServiceExporter($this, $fp); $exporter->init(); if (!$exporter->valid()) { $this->addQueuedExporter($exporter); $job->addNotice("NagiosImportEngine queueing up Service exporter until dependencies are valid."); } else { if (!$exporter->export()) { $job->addError("Service Exporter failed export. Bailing out of job..."); return false; } } // Dependency Configuration $fp = @fopen($this->exportDir . "/objects/dependencies.cfg", "w"); if (!$fp) { $job->addError("Unable to open " . $this->exportDir . "/objects/dependencies.cfg for writing."); return false; } $exporter = new NagiosDependencyExporter($this, $fp); $exporter->init(); if (!$exporter->valid()) { $this->addQueuedExporter($exporter); $job->addNotice("NagiosImportEngine queueing up Dependency exporter until dependencies are valid."); } else { if (!$exporter->export()) { $job->addError("Dependency Exporter failed export. Bailing out of job..."); return false; } } // Escalation Configuration $fp = @fopen($this->exportDir . "/objects/escalations.cfg", "w"); if (!$fp) { $job->addError("Unable to open " . $this->exportDir . "/objects/escalations.cfg for writing."); return false; } $exporter = new NagiosEscalationExporter($this, $fp); $exporter->init(); if (!$exporter->valid()) { $this->addQueuedExporter($exporter); $job->addNotice("NagiosImportEngine queueing up Escalation exporter until dependencies are valid."); } else { if (!$exporter->export()) { $job->addError("Escalation Exporter failed export. Bailing out of job..."); return false; } } $job->addNotice("Finished exporting objects."); // Finished exporting, let's check if we need to do the preflight if ($config->getVar("preflight_check")) { exec($this->verifyCmd . " -v " . $this->exportDir . "/nagios.cfg", $output, $retVal); if ($retVal != 0) { if ($retVal == 127) { $job->addError("The command to verify your configuration: " . $this->verifyCmd . " was not found (127)."); } else { $job->addError("Nagios Sanity Check Failed. Did not write configuration files to production. Output is as follows:"); foreach ($output as $outputLine) { $job->addError($outputLine); } } $job->addError("Export failed."); return false; } else { $job->addNotice("Nagios Sanity Check Passed."); } } // Now need to re-export main configuration file, so config dir's point // to right location $fp = @fopen($this->exportDir . "/nagios.cfg", "w"); if (!$fp) { $job->addError("Unable to open " . $this->exportDir . "/nagios.cfg for writing."); return false; } $exporter = new NagiosMainExporter($this, $fp); $exporter->init(); if (!$exporter->valid()) { $this->addQueuedExporter($exporter); $job->addNotice("NagiosImportEngine queueing up Main exporter until dependencies are valid."); } else { if (!$exporter->export()) { $job->addError("Main Exporter failed export. Bailing out of job..."); return false; } } // Move the configuration files to the appropriate place. $mainConfiguration = NagiosMainConfigurationPeer::doSelectOne(new Criteria()); if (!$this->dir_copy($this->exportDir, $mainConfiguration->getConfigDir())) { $job->addError("Unable to copy configuration files to : " . $mainConfiguration->getConfigDir()); $job->addError("Export failed."); return false; } // Check if we have to restart if ($config->getVar("restart_nagios")) { $output = null; exec($this->restartCmd, $output, $retVal); if ($retVal != 0) { if ($retVal == 127) { $job->addError("The command to restart Nagios: " . $this->restartCmd . " was not found (127)."); } else { $job->addError("Nagios Restart Failed. You need to manually restart Nagios"); foreach ($output as $outputLine) { $job->addError($outputLine); } } $job->addError("Restart failed."); return false; } $job->addNotice("Nagios Restarted Successfully."); } return true; }
public function import() { $job = $this->getEngine()->getJob(); $config = $this->getEngine()->getConfig(); $mainCfg = new NagiosMainConfiguration(); $segment = $this->getSegment(); $values = $segment->getValues(); $fileName = $segment->getFilename(); // Setup the configuration directory for the new config file to match the file we imported $mainCfg->setConfigDir(dirname(realpath($fileName))); foreach ($values as $key => $entries) { foreach ($entries as $entry) { $value = $entry['value']; $lineNum = $entry['line']; if (key_exists($key, $this->fieldMethods) && $this->fieldMethods[$key] != '') { // Okay, let's check that the method DOES exist if (!method_exists($mainCfg, $this->fieldMethods[$key])) { $job->addError("Method " . $this->fieldMethods[$key] . " does not exist for variable: " . $key . " on line " . $lineNum . " in file " . $fileName); if (!$config->getVar('continue_error')) { return false; } } else { call_user_method($this->fieldMethods[$key], $mainCfg, $value); } } } } // If we got here, it's safe to delete the existing main config and save the new one $oldConfig = NagiosMainConfigurationPeer::doSelectOne(new Criteria()); if ($oldConfig) { $oldConfig->delete(); } $mainCfg->save(); $mainCfg->clearAllReferences(true); $job->addNotice("NagiosMainImporter finished importing main configuration."); return true; }