public function export()
 {
     $configurationFile = new ConfigurationFile('configuration/services.cfg', ConfigurationFile::TYPE_CONFIGURATION_FILE);
     /* @var $service Service */
     foreach ($this->repository->findAll() as $service) {
         if (!$service->isEnabled()) {
             continue;
         }
         $contactNames = array_map(function (Contact $contact) {
             return $contact->getName();
         }, $service->getContacts());
         $commandName = $service->getCommand()->getName();
         $commandArgs = array_map(function (ServiceCommandArgument $argument) use($service) {
             return $this->escapeShellArg($service, $argument->getValue());
         }, $service->getArguments());
         $checkCommand = $commandName . (count($commandArgs) === 0 ? '' : '!' . implode('!', $commandArgs));
         $definition = new ObjectDefinition('service');
         $definition->addDirective('use', 'nagadmin-service');
         $definition->addDirective('host_name', $service->getHost()->getName());
         $definition->addDirective('service_description', $service->getName());
         $definition->addDirective('check_command', $checkCommand);
         $definition->addDirective('max_check_attempts', $service->getMaxCheckAttempts());
         $definition->addDirective('check_interval', $service->getCheckInterval());
         $definition->addDirective('retry_interval', $service->getRetryInterval());
         $definition->addDirective('notification_interval', $service->getNotificationInterval());
         if (count($contactNames) > 0) {
             $definition->addDirective('contacts', implode(",", $contactNames));
         }
         $configurationFile->addObjectDefinition($definition);
     }
     return $configurationFile;
 }
 public function export()
 {
     $configurationFile = new ConfigurationFile('configuration/auto_sevice_deps.cfg', ConfigurationFile::TYPE_CONFIGURATION_FILE);
     /* @var $host Host */
     foreach ($this->hostRepository->findAll() as $host) {
         $services = $this->serviceRepository->findByHost($host);
         $masterServices = array();
         $dependentServices = array();
         /* @var $service Service */
         foreach ($services as $service) {
             if (!$service->isEnabled()) {
                 continue;
             }
             if ($this->isMasterService($service)) {
                 $masterServices[] = $service;
             } else {
                 $dependentServices[] = $service;
             }
         }
         /* @var $masterService Service */
         /* @var $dependentService Service */
         foreach ($masterServices as $masterService) {
             foreach ($dependentServices as $dependentService) {
                 $definition = new ObjectDefinition('servicedependency');
                 $definition->addDirective('host_name', $host->getName());
                 $definition->addDirective('service_description', $masterService->getName());
                 $definition->addDirective('dependent_host_name', $host->getName());
                 $definition->addDirective('dependent_service_description', $dependentService->getName());
                 $definition->addDirective('notification_failure_criteria', 'w,c,u,p');
                 $configurationFile->addObjectDefinition($definition);
             }
         }
     }
     return $configurationFile;
 }
 public function export()
 {
     $configurationFile = new ConfigurationFile('resource.cfg', ConfigurationFile::TYPE_RESOURCE_FILE);
     $resource = $this->repository->getResource();
     foreach ($resource->getVariables() as $name => $value) {
         $configurationFile->addVariable($name, $value);
     }
     return $configurationFile;
 }
 public function export()
 {
     $configurationFile = new ConfigurationFile('configuration/commands.cfg', ConfigurationFile::TYPE_CONFIGURATION_FILE);
     /* @var $command Command */
     foreach ($this->repository->findAll() as $command) {
         $definition = new ObjectDefinition('command');
         $definition->addDirective('command_name', $command->getName());
         $definition->addDirective('command_line', $command->getLine());
         $configurationFile->addObjectDefinition($definition);
     }
     return $configurationFile;
 }
 public function export()
 {
     $configurationFile = new ConfigurationFile('configuration/timeperiods.cfg', ConfigurationFile::TYPE_CONFIGURATION_FILE);
     /* @var $timePeriod TimePeriod */
     foreach ($this->repository->findAll() as $timePeriod) {
         $definition = new ObjectDefinition('timeperiod');
         $definition->addDirective('timeperiod_name', $timePeriod->getName());
         $definition->addDirective('alias', $timePeriod->getName());
         foreach ($timePeriod->getRules() as $rule) {
             $definition->addDirective($rule->getDateRange(), $rule->getTimeRange());
         }
         $configurationFile->addObjectDefinition($definition);
     }
     return $configurationFile;
 }
 public function export()
 {
     $configurationFile = new ConfigurationFile('configuration/hosts.cfg', ConfigurationFile::TYPE_CONFIGURATION_FILE);
     /* @var $host Host */
     foreach ($this->repository->findAll() as $host) {
         $hostGroupNames = $host->getGroups();
         $definition = new ObjectDefinition('host');
         $definition->addDirective('use', 'nagadmin-host');
         $definition->addDirective('host_name', $host->getName());
         $definition->addDirective('alias', $host->getName());
         $definition->addDirective('address', $host->getAddress());
         if (count($hostGroupNames) > 0) {
             $definition->addDirective('hostgroups', implode(',', $hostGroupNames));
         }
         $configurationFile->addObjectDefinition($definition);
     }
     return $configurationFile;
 }
 public function export()
 {
     $configurationFile = new ConfigurationFile('configuration/contacts.cfg', ConfigurationFile::TYPE_CONFIGURATION_FILE);
     /* @var $contact Contact */
     foreach ($this->repository->findAll() as $contact) {
         $definition = new ObjectDefinition('contact');
         $definition->addDirective('use', 'nagadmin-contact');
         $definition->addDirective('contact_name', $contact->getName());
         $definition->addDirective('email', $contact->getEmail());
         $definition->addDirective('host_notification_period', $contact->getTimePeriod()->getName());
         $definition->addDirective('service_notification_period', $contact->getTimePeriod()->getName());
         $definition->addDirective('service_notification_commands', $contact->getServiceNotificationCommand()->getName());
         foreach ($contact->getAddresses() as $slot => $address) {
             $definition->addDirective('address' . $slot, $address);
         }
         $configurationFile->addObjectDefinition($definition);
     }
     return $configurationFile;
 }
 public function export()
 {
     $configurationFile = new ConfigurationFile('configuration/hostgroups.cfg', ConfigurationFile::TYPE_CONFIGURATION_FILE);
     $groupsMap = array();
     /* @var $host Host */
     foreach ($this->repository->findAll() as $host) {
         $hostGroupNames = $host->getGroups();
         foreach ($host->getGroups() as $groupName) {
             $groupsMap[$groupName] = true;
         }
     }
     ksort($groupsMap);
     foreach (array_keys($groupsMap) as $groupName) {
         $definition = new ObjectDefinition('hostgroup');
         $definition->addDirective('hostgroup_name', $groupName);
         $definition->addDirective('alias', $groupName);
         $configurationFile->addObjectDefinition($definition);
     }
     return $configurationFile;
 }
 private function createMainConfigFile($path, array $configurationFiles, $checkResultPath, $logFilePath)
 {
     $mainConfigFile = new ConfigurationFile('nagios.cfg', ConfigurationFile::TYPE_CONFIGURATION_FILE);
     $mainConfigFile->addVariable('check_result_path', $checkResultPath);
     $mainConfigFile->addVariable('log_file', $logFilePath);
     $mainConfigFile->addVariable('illegal_macro_output_chars', '`~$&|\'"<>');
     foreach ($configurationFiles as $configurationFile) {
         $type = $configurationFile->getType();
         if ($type === ConfigurationFile::TYPE_CONFIGURATION_FILE) {
             $mainConfigFile->addVariable('cfg_file', $path . '/' . $configurationFile->getPath());
         } else {
             if ($type === ConfigurationFile::TYPE_RESOURCE_FILE) {
                 $mainConfigFile->addVariable('resource_file', $path . '/' . $configurationFile->getPath());
             }
         }
     }
     return $mainConfigFile;
 }
 public function export()
 {
     $configurationFile = new ConfigurationFile('configuration/internal.cfg', ConfigurationFile::TYPE_CONFIGURATION_FILE);
     $definition = new ObjectDefinition('timeperiod');
     $definition->addDirective('timeperiod_name', 'nagadmin-24x7');
     $definition->addDirective('alias', 'nagadmin-24x7');
     foreach (array('monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday') as $dayName) {
         $definition->addDirective($dayName, '00:00-24:00');
     }
     $configurationFile->addObjectDefinition($definition);
     $definition = new ObjectDefinition('command');
     $definition->addDirective('command_name', 'nagadmin-check-dummy-ok');
     $definition->addDirective('command_line', '$USER1$/check_dummy 0 "Nagadmin does not perform host checks. All hosts are forced to an OK status."');
     $configurationFile->addObjectDefinition($definition);
     $definition = new ObjectDefinition('command');
     $definition->addDirective('command_name', 'nagadmin-notify-dummy');
     $definition->addDirective('command_line', 'true');
     $configurationFile->addObjectDefinition($definition);
     $definition = new ObjectDefinition('contact');
     $definition->addDirective('name', 'nagadmin-contact');
     $definition->addDirective('host_notification_period', 'nagadmin-24x7');
     $definition->addDirective('host_notification_commands', 'nagadmin-notify-dummy');
     $definition->addDirective('service_notification_period', 'nagadmin-24x7');
     $definition->addDirective('service_notification_options', 'w,u,c,r,f,s');
     $definition->addDirective('host_notification_options', 'd,u,r,f,s');
     $definition->addDirective('register', 0);
     //Not a real contact, just a template. Do not really register!
     $configurationFile->addObjectDefinition($definition);
     //A fake/dummy contact to prevent Nagios from complaining when there are no real contacts
     //defined in the system (which is the initial state after installation).
     //Nagios should also run without contacts defined - if the notification features are not used.
     $definition = new ObjectDefinition('contact');
     $definition->addDirective('use', 'nagadmin-contact');
     $definition->addDirective('contact_name', 'nagadmin-dummy-contact');
     $definition->addDirective('email', 'root@localhost');
     $definition->addDirective('service_notification_commands', 'nagadmin-notify-dummy');
     $configurationFile->addObjectDefinition($definition);
     $definition = new ObjectDefinition('host');
     $definition->addDirective('name', 'nagadmin-host');
     $definition->addDirective('notifications_enabled', 1);
     $definition->addDirective('event_handler_enabled', 1);
     $definition->addDirective('flap_detection_enabled', 1);
     $definition->addDirective('process_perf_data', 1);
     $definition->addDirective('retain_nonstatus_information', 1);
     $definition->addDirective('notification_period', 'nagadmin-24x7');
     $definition->addDirective('check_period', 'nagadmin-24x7');
     $definition->addDirective('check_command', 'nagadmin-check-dummy-ok');
     $definition->addDirective('max_check_attempts', 10);
     $definition->addDirective('register', 0);
     //Not a real host, just a template. Do not really register!
     $configurationFile->addObjectDefinition($definition);
     $definition = new ObjectDefinition('service');
     $definition->addDirective('name', 'nagadmin-service');
     $definition->addDirective('active_checks_enabled', 1);
     $definition->addDirective('passive_checks_enabled', 1);
     $definition->addDirective('parallelize_check', 1);
     $definition->addDirective('obsess_over_service', 0);
     $definition->addDirective('notifications_enabled', 1);
     $definition->addDirective('event_handler_enabled', 1);
     $definition->addDirective('flap_detection_enabled', 1);
     $definition->addDirective('process_perf_data', 1);
     $definition->addDirective('retain_status_information', 1);
     $definition->addDirective('retain_nonstatus_information', 1);
     $definition->addDirective('is_volatile', 0);
     $definition->addDirective('check_period', 'nagadmin-24x7');
     $definition->addDirective('notification_period', 'nagadmin-24x7');
     $definition->addDirective('notification_options', 'w,u,c,r');
     $definition->addDirective('register', 0);
     //Not a real host, just a template. Do not really register!
     $configurationFile->addObjectDefinition($definition);
     return $configurationFile;
 }