function contact_validate($app, $deployment, $contactInfo) { foreach ($contactInfo as $key => $value) { switch ($key) { case "use": case "host_notification_period": case "service_notification_period": case "host_notification_commands": case "service_notification_commands": validateForbiddenChars($app, $deployment, '/[^\\w.-]/s', $key, $value); break; case "retain_status_information": case "retain_nonstatus_information": case "host_notifications_enabled": case "service_notifications_enabled": case "can_submit_commands": validateBinary($app, $deployment, $key, $value); break; case "host_notification_options": $opts = validateOptions($app, $deployment, $key, $value, array('d', 'u', 'r', 's', 'n'), true); $contactInfo[$key] = $opts; break; case "service_notification_options": $opts = validateOptions($app, $deployment, $key, $value, array('w', 'u', 'c', 'r', 's', 'n'), true); $contactInfo[$key] = $opts; break; case "email": validateEmail($app, $deployment, $key, $value); break; case "pager": if (filter_var($value, FILTER_VALIDATE_EMAIL) === false) { if (!preg_match("/^(?[2-9][0-8][0-9])?-[2-9][0-0]{2}-[0-9]{4}\$/", $value)) { $apiResponse = new APIViewData(1, $deployment, "Unable use pager number provided, the value provided doesn't match the regex for pager or email address"); $apiResponse->setExtraResponseData('parameter', $key); $apiResponse->setExtraResponseData('parameter-value', $value); $apiResponse->setExtraResponseData('parameter-pager-regex', "/^(?[2-9][0-8][0-9])?-[2-9][0-0]{2}-[0-9]{4}\$/"); $app->halt(404, $apiResponse->returnJson()); } } break; case "contactgroups": if (is_array($value)) { $value = implode(',', $value); } validateForbiddenChars($app, $deployment, '/[^\\w.-]/s', $key, $value); break; default: break; } } return $contactInfo; }
} if (empty($results)) { $apiResponse = new APIViewData(1, $deployment, "Unable to detect service groups specified: {$servicegroup}"); $app->halt(404, $apiResponse->returnJson()); } else { $apiResponse = new APIViewData(0, $deployment, false); $apiResponse->setExtraResponseData('service_groups', $results); } } else { if (RevDeploy::existsDeploymentSvcGroup($deployment, $servicegroup, $deployRev) === false) { $apiResponse = new APIViewData(1, $deployment, "Unable to detect service groups specified: {$servicegroup}"); $app->halt(404, $apiResponse->returnJson()); } else { $serviceGroupsInfo = RevDeploy::getDeploymentSvcGroup($deployment, $servicegroup, $deployRev); $apiResponse = new APIViewData(0, $deployment, false); $apiResponse->setExtraResponseData('service_group', $serviceGroupsInfo); } } $apiResponse->printJson(); })->name('saigon-api-get-service-groups'); $app->post('/sapi/servicegroup/:deployment', function ($deployment) use($app) { check_deployment_exists($app, $deployment); check_auth($app, $deployment); check_revision_status($deployment); $request = $app->request(); $contentType = $request->headers('Content-Type'); if ($contentType == 'application/json') { $serviceGroupsInfo = $request->getBody(); $serviceGroupsInfo = json_decode($serviceGroupsInfo, true); } elseif (preg_match("/form-(data|urlencoded)/", $contentType)) { $keys = array('name', 'alias', 'notes', 'notes_url', 'action_url');
} if (empty($results)) { $apiResponse = new APIViewData(1, $deployment, "Unable to detect services specified: {$service}"); $app->halt(404, $apiResponse->returnJson()); } else { $apiResponse = new APIViewData(0, $deployment, false); $apiResponse->setExtraResponseData('services', $results); } } else { if (RevDeploy::existsDeploymentSvc($deployment, $service, $deployRev) === false) { $apiResponse = new APIViewData(1, $deployment, "Unable to detect service specified: {$service}"); $app->halt(404, $apiResponse->returnJson()); } else { $serviceInfo = RevDeploy::getDeploymentSvc($deployment, $service, $deployRev); $apiResponse = new APIViewData(0, $deployment, false); $apiResponse->setExtraResponseData('service', $serviceInfo); } } $apiResponse->printJson(); })->name('saigon-api-get-service'); $app->post('/sapi/service/:deployment', function ($deployment) use($app) { check_deployment_exists($app, $deployment); check_auth($app, $deployment); check_revision_status($deployment); $request = $app->request(); $contentType = $request->headers('Content-Type'); if ($contentType == 'application/json') { $serviceInfo = $request->getBody(); $serviceInfo = json_decode($serviceInfo, true); } elseif (preg_match("/form-(data|urlencoded)/", $contentType)) { $keys = array('name', 'use', 'service_description', 'servicegroups', 'is_volatile', 'check_command', 'initial_state', 'max_check_attempts', 'check_interval', 'retry_interval', 'active_checks_enabled', 'passive_checks_enabled', 'check_period', 'obsess_over_service', 'check_freshness', 'freshness_threshold', 'event_handler', 'event_handler_enabled', 'low_flap_threshold', 'high_flap_threshold', 'flap_detection_enabled', 'flap_detection_options', 'process_perf_data', 'retain_status_information', 'retain_nonstatus_information', 'notification_interval', 'first_notification_delay', 'notification_period', 'notification_options', 'notifications_enabled', 'contacts', 'contact_groups', 'stalking_options', 'icon_image', 'icon_image_alt', 'parallelize_check', 'notes', 'notes_url', 'action_url');
$apiResponse = new APIViewData(1, $deployment, "Unable to detect server parameter (nagios server to submit results too)"); $app->halt(404, $apiResponse->returnJson()); } } else { $nagiosServer = $eventInfo['server']; } $msg = $eventInfo['host'] . ","; if ($type == 'service') { $msg .= $eventInfo['service'] . ","; } $msg .= $eventInfo['exitcode'] . "," . $eventInfo['output']; $eventSubmission = API_EVENT_SUBMISSION; if ($eventSubmission == 'enqueue') { NagPhean::init(BEANSTALKD_SERVER, 'events', true); NagPhean::addJob('events', json_encode(array('server' => $nagiosServer, 'type' => 'nsca', 'data' => $msg)), 1024, 0, 10); $apiResponse = new APIViewData(0, $deployment, $msg); $apiResponse->setExtraResponseData('eventsubmission', $eventSubmission); $apiResponse->printJson(); } else { $nscabin = '/usr/sbin/send_nsca'; if (file_exists($nscabin)) { shell_exec("echo {$msg} | {$nscabin} -H {$nagiosServer} -d , -c /etc/nagios/send_nsca.cfg"); $apiResponse = new APIViewData(0, $deployment, $msg); $apiResponse->setExtraResponseData('eventsubmission', $eventSubmission); $apiResponse->printJson(); } else { $apiResponse = new APIViewData(1, $deployment, "Unable to detect /usr/sbin/send_nsca binary needed for communication"); $app->halt(404, $apiResponse->returnJson()); } } })->via('GET', 'POST')->name('saigon-api-create-nsca-event')->conditions(array('type' => '(host|service)'));
} if (empty($results)) { $apiResponse = new APIViewData(1, $deployment, "Unable to detect contact templates specified: {$contacttemplate}"); $app->halt(404, $apiResponse->returnJson()); } else { $apiResponse = new APIViewData(0, $deployment, false); $apiResponse->setExtraResponseData('contact_templates', $results); } } else { if (RevDeploy::existsDeploymentContactTemplate($deployment, $contacttemplate, $deployRev) === false) { $apiResponse = new APIViewData(1, $deployment, "Unable to detect contact template specified: {$contacttemplate}"); $app->halt(404, $apiResponse->returnJson()); } else { $contactTemplateInfo = RevDeploy::getDeploymentContactTemplate($deployment, $contacttemplate, $deployRev); $apiResponse = new APIViewData(0, $deployment, false); $apiResponse->setExtraResponseData('contact_template', $contactTemplateInfo); } } $apiResponse->printJson(); })->name('saigon-api-get-contact-template'); $app->post('/sapi/contacttemplate/:deployment', function ($deployment) use($app) { check_deployment_exists($app, $deployment); check_auth($app, $deployment); check_revision_status($deployment); $request = $app->request(); $contentType = $request->headers('Content-Type'); if ($contentType == 'application/json') { $contactTemplateInfo = $request->getBody(); $contactTemplateInfo = json_decode($contactTemplateInfo, true); } elseif (preg_match("/form-(data|urlencoded)/", $contentType)) { $keys = array('name', 'alias', 'use', 'retain_status_information', 'retain_nonstatus_information', 'host_notifications_enabled', 'service_notifications_enabled', 'host_notification_period', 'service_notification_period', 'host_notification_options', 'service_notification_options', 'host_notification_commands', 'service_notification_commands', 'can_submit_commands');
$apiResponse->setExtraResponseData('deleted', $results); $apiResponse->printJson(); } else { $apiResponse = new APIViewData(1, $deployment, "Unable to detect dynamic host information specified in the data store"); $app->halt(404, $apiResponse->returnJson()); } } elseif ($type == 'static') { if (!isset($hostInfo['host']) || empty($hostInfo['host'])) { $apiResponse = new APIViewData(1, $deployment, "Unable to detect host parameter (expecting hostname or fqdn)"); $app->halt(404, $apiResponse->returnJson()); } elseif (!isset($hostInfo['ip']) || empty($hostInfo['ip'])) { $apiResponse = new APIViewData(1, $deployment, "Unable to detect ip parameter (expecting to match /^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\$/)"); $app->halt(404, $apiResponse->returnJson()); } elseif (!preg_match("/^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\$/", $hostInfo['ip'])) { $apiResponse = new APIViewData(1, $deployment, "Unable to use ip parameter (expecting to match /^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\$/)"); $app->halt(404, $apiResponse->returnJson()); } $results = RevDeploy::delDeploymentStaticHost($deployment, NagMisc::encodeIP($hostInfo['ip'])); if (!empty($results)) { $apiResponse = new APIViewData(0, $deployment, false); $apiResponse->setExtraResponseData('deleted', $results); $apiResponse->printJson(); } else { $apiResponse = new APIViewData(1, $deployment, "Unable to detect static host information specified in the data store"); $app->halt(404, $apiResponse->returnJson()); } } else { $apiResponse = new APIViewData(1, $deployment, "Unsure of how you got here, as a higher route should have blocked you, check your host type for processing"); $app->halt(404, $apiResponse->returnJson()); } })->name('saigon-api-deployment-delete-host')->conditions(array('type' => '(dynamic|static)'));
*/ $app->get('/sapi/configs/:deployment/nagios(/:staged)', function ($deployment, $staged = false) use($app) { check_deployment_exists($app, $deployment); // Load up Current Commands or Staged Commands if ($staged == 1) { $deployRev = RevDeploy::getDeploymentNextRev($deployment); if ($deployRev === false) { $apiResponse = new APIViewData(1, $deployment, "Unable to detect staged revision to reference"); $app->halt(404, $apiResponse->returnJson()); } } else { $deployRev = RevDeploy::getDeploymentRev($deployment); } $apiResponse = new APIViewData(0, $deployment, false); if (RevDeploy::existsDeploymentNagiosCfg($deployment, $deployRev) === true) { $apiResponse->setExtraResponseData('nagios_config', RevDeploy::getDeploymentNagiosCfg($deployment, $deployRev)); } else { $apiResponse->setExtraResponseData('nagios_config', array('accept_passive_host_checks' => 1, 'accept_passive_service_checks' => 1, 'cached_host_check_horizon' => 15, 'cached_service_check_horizon' => 15, 'cfg_dir' => base64_encode('/usr/local/nagios/etc/objects'), 'check_external_commands' => 1, 'check_for_orphaned_hosts' => 1, 'check_for_orphaned_services' => 1, 'check_host_freshness' => 1, 'check_result_path' => base64_encode('/usr/local/nagios/var/spool/checkresults'), 'check_result_reaper_frequency' => 10, 'check_service_freshness' => 1, 'command_check_interval' => -1, 'command_file' => base64_encode('/usr/local/nagios/var/rw/nagios.cmd'), 'enable_event_handlers' => 1, 'enable_notifications' => 1, 'enable_predictive_host_dependency_checks' => 1, 'enable_predictive_service_dependency_checks' => 1, 'event_broker_options' => -1, 'event_handler_timeout' => 30, 'execute_host_checks' => 1, 'execute_service_checks' => 1, 'external_command_buffer_slots' => 4096, 'host_check_timeout' => 30, 'host_freshness_check_interval' => 60, 'illegal_macro_output_chars' => base64_encode('`~$&|\'"<>'), 'illegal_object_name_chars' => base64_encode('`~!$%^&*|\'"<>?,()='), 'lock_file' => base64_encode('/usr/local/nagios/var/nagios.lock'), 'log_archive_path' => base64_encode('/usr/local/nagios/var/archives'), 'log_event_handlers' => 1, 'log_external_commands' => 1, 'log_file' => base64_encode('/usr/local/nagios/var/nagios.log'), 'log_host_retries' => 1, 'log_initial_states' => 0, 'log_notifications' => 1, 'log_passive_checks' => 1, 'log_rotation_method' => 'd', 'log_service_retries' => 1, 'max_check_result_file_age' => 3600, 'max_check_result_reaper_time' => 30, 'nagios_group' => 'nagios', 'nagios_user' => 'nagios', 'notification_timeout' => 30, 'object_cache_file' => base64_encode('/usr/local/nagios/var/objects.cache'), 'precached_object_file' => base64_encode('/usr/local/nagios/var/objects.precache'), 'resource_file' => base64_encode('/usr/local/nagios/etc/resource.cfg'), 'retain_state_information' => 1, 'retention_update_interval' => 60, 'service_check_timeout' => 60, 'service_freshness_check_interval' => 60, 'soft_state_dependencies' => 0, 'state_retention_file' => base64_encode('/usr/local/nagios/var/retention.dat'), 'status_file' => base64_encode('/usr/local/nagios/var/status.dat'), 'status_update_interval' => 10, 'temp_file' => base64_encode('/usr/local/nagios/var/nagios.tmp'), 'temp_path' => base64_encode('/tmp'), 'use_large_installation_tweaks' => 0, 'use_retained_program_state' => 1, 'use_retained_scheduling_info' => 1, 'use_syslog' => 1, 'additional_freshness_latency' => 15, 'admin_email' => '*****@*****.**', 'admin_pager' => '*****@*****.**', 'auto_reschedule_checks' => 0, 'auto_rescheduling_interval' => 30, 'auto_rescheduling_window' => 180, 'bare_update_check' => 0, 'check_for_updates' => 0, 'daemon_dumps_core' => 0, 'date_format' => 'us', 'debug_file' => base64_encode('/usr/local/nagios/var/nagios.debug'), 'debug_level' => 0, 'debug_verbosity' => 1, 'enable_embedded_perl' => 1, 'enable_environment_macros' => 0, 'enable_flap_detection' => 0, 'high_host_flap_threshold' => 20.0, 'high_service_flap_threshold' => 20.0, 'host_inter_check_delay_method' => 's', 'interval_length' => 60, 'low_host_flap_threshold' => 5.0, 'low_service_flap_threshold' => 5.0, 'max_concurrent_checks' => 0, 'max_host_check_spread' => 30, 'max_service_check_spread' => 30, 'max_debug_file_size' => 1000000, 'obsess_over_hosts' => 0, 'obsess_over_services' => 0, 'ocsp_timeout' => 5, 'ochp_timeout' => 5, 'ocsp_command' => base64_encode('ocsp_command'), 'ochp_command' => base64_encode('ochp_command'), 'p1_file' => base64_encode('/usr/local/nagios/bin/p1.pl'), 'passive_host_checks_are_soft' => 0, 'retained_contact_host_attribute_mask' => 0, 'retained_contact_service_attribute_mask' => 0, 'retained_host_attribute_mask' => 0, 'retained_process_host_attribute_mask' => 0, 'retained_process_service_attribute_mask' => 0, 'retained_service_attribute_mask' => 0, 'service_inter_check_delay_method' => 's', 'service_interleave_factor' => 's', 'sleep_time' => 0.25, 'translate_passive_host_checks' => 0, 'use_aggressive_host_checking' => 0, 'use_embedded_perl_implicitly' => 1, 'use_regexp_matching' => 0, 'use_true_regexp_matching' => 0, 'process_performance_data' => 0, 'perfdata_timeout' => 5, 'host_perfdata_command' => base64_encode('process-host-perfdata'), 'service_perfdata_command' => base64_encode('process-service-perfdata'), 'host_perfdata_file' => base64_encode('/tmp/host-perfdata'), 'service_perfdata_file' => base64_encode('/tmp/service-perfdata'), 'host_perfdata_file_template' => base64_encode('[HOSTPERFDATA]\\t$TIMET$\\t$HOSTNAME$\\t$HOSTEXECUTIONTIME$\\t$HOSTOUTPUT$\\t$HOSTPERFDATA$'), 'service_perfdata_file_template' => base64_encode('[SERVICEPERFDATA]\\t$TIMET$\\t$HOSTNAME$\\t$SERVICEDESC$\\t$SERVICEEXECUTIONTIME$\\t$SERVICELATENCY$\\t$SERVICEOUTPUT$\\t$SERVICEPERFDATA$'), 'host_perfdata_file_mode' => 'a', 'service_perfdata_file_mode' => 'a', 'host_perfdata_file_processing_interval' => 0, 'service_perfdata_file_processing_interval' => 0, 'host_perfdata_file_processing_command' => base64_encode('process-host-perfdata-file'), 'service_perfdata_file_processing_command' => base64_encode('process-service-perfdata-file'))); } $apiResponse->printJson(); })->name('saigon-api-get-nagios-config'); $app->post('/sapi/configs/:deployment/nagios', function ($deployment) use($app) { check_deployment_exists($app, $deployment); check_auth($app, $deployment); $keys = array('accept_passive_host_checks', 'accept_passive_service_checks', 'cached_host_check_horizon', 'cached_service_check_horizon', 'cfg_dir', 'check_external_commands', 'check_for_orphaned_hosts', 'check_for_orphaned_services', 'check_host_freshness', 'check_result_path', 'check_result_reaper_frequency', 'check_service_freshness', 'command_check_interval', 'command_file', 'enable_event_handlers', 'enable_notifications', 'enable_predictive_host_dependency_checks', 'enable_predictive_service_dependency_checks', 'event_broker_options', 'event_handler_timeout', 'execute_host_checks', 'execute_service_checks', 'external_command_buffer_slots', 'host_check_timeout', 'host_freshness_check_interval', 'illegal_macro_output_chars', 'illegal_object_name_chars', 'lock_file', 'log_archive_path', 'log_event_handlers', 'log_external_commands', 'log_file', 'log_host_retries', 'log_initial_states', 'log_notifications', 'log_passive_checks', 'log_rotation_method', 'log_service_retries', 'max_check_result_file_age', 'max_check_result_reaper_time', 'nagios_group', 'nagios_user', 'notification_timeout', 'object_cache_file', 'precached_object_file', 'resource_file', 'retain_state_information', 'retention_update_interval', 'service_check_timeout', 'service_freshness_check_interval', 'soft_state_dependencies', 'state_retention_file', 'status_file', 'status_update_interval', 'temp_file', 'temp_path', 'use_large_installation_tweaks', 'use_retained_program_state', 'use_retained_scheduling_info', 'use_syslog', 'additional_freshness_latency', 'admin_email', 'admin_pager', 'auto_reschedule_checks', 'auto_rescheduling_interval', 'auto_rescheduling_window', 'bare_update_check', 'check_for_updates', 'daemon_dumps_core', 'date_format', 'debug_file', 'debug_level', 'debug_verbosity', 'enable_embedded_perl', 'enable_environment_macros', 'enable_flap_detection', 'high_host_flap_threshold', 'high_service_flap_threshold', 'host_inter_check_delay_method', 'interval_length', 'low_host_flap_threshold', 'low_service_flap_threshold', 'max_concurrent_checks', 'max_host_check_spread', 'max_service_check_spread', 'max_debug_file_size', 'obsess_over_hosts', 'obsess_over_services', 'ocsp_timeout', 'ochp_timeout', 'ocsp_command', 'ochp_command', 'p1_file', 'passive_host_checks_are_soft', 'retained_contact_host_attribute_mask', 'retained_contact_service_attribute_mask', 'retained_host_attribute_mask', 'retained_process_host_attribute_mask', 'retained_process_service_attribute_mask', 'retained_service_attribute_mask', 'service_inter_check_delay_method', 'service_interleave_factor', 'sleep_time', 'translate_passive_host_checks', 'use_aggressive_host_checking', 'use_embedded_perl_implicitly', 'use_regexp_matching', 'use_true_regexp_matching', 'process_performance_data', 'perfdata_timeout', 'host_perfdata_command', 'service_perfdata_command', 'host_perfdata_file', 'service_perfdata_file', 'host_perfdata_file_template', 'service_perfdata_file_template', 'host_perfdata_file_mode', 'service_perfdata_file_mode', 'host_perfdata_file_processing_interval', 'service_perfdata_file_processing_interval', 'host_perfdata_file_processing_command', 'service_perfdata_file_processing_command', 'broker_modules'); $request = $app->request(); $contentType = $request->headers('Content-Type'); if ($contentType == 'application/json') { $nagiosConfigInfo = $request->getBody(); $nagiosConfigInfo = json_decode($nagiosConfigInfo, true); } elseif (preg_match("/form-(data|urlencoded)/", $contentType)) {
$results[$plugin] = $pluginData; } } if (!empty($results)) { $apiResponse = new APIViewData(0, $deployment, false); $apiResponse->setExtraResponseData('plugins', $results); $apiResponse->printJson(); } else { $apiResponse = new APIViewData(1, $deployment, "Unable to detect nagios plugin(s) specified: {$nagiosPlugin}"); $app->halt(404, $apiResponse->returnJson()); } } else { $pluginData = RevDeploy::getDeploymentNagiosPlugin($deployment, $nagiosPlugin, $deployRev); if ($pluginData !== false) { $apiResponse = new APIViewData(0, $deployment, false); $apiResponse->setExtraResponseData('plugin', $pluginData); $apiResponse->printJson(); } else { $apiResponse = new APIViewData(1, $deployment, "Unable to detect nagios plugin specified: {$nagiosPlugin}"); $app->halt(404, $apiResponse->returnJson()); } } })->name('saigon-api-get-nagios-plugin'); $app->delete('/sapi/nagiosplugin/:deployment/:nagiosplugin', function ($deployment, $nagiosPlugin) use($app) { check_deployment_exists($app, $deployment); check_auth($app, $deployment); check_revision_status($deployment); $deployRev = RevDeploy::getDeploymentNextRev($deployment); if (preg_match('/,/', $nagiosPlugin)) { $results = array(); $plugins = preg_split('/\\s?,\\s?/', $nagiosPlugin);
} if (empty($results)) { $apiResponse = new APIViewData(1, $deployment, "Unable to detect contact groups specified: {$contactgroup}"); $app->halt(404, $apiResponse->returnJson()); } else { $apiResponse = new APIViewData(0, $deployment, false); $apiResponse->setExtraResponseData('contact_groups', $results); } } else { if (RevDeploy::existsDeploymentContactGroup($deployment, $contactgroup, $deployRev) === false) { $apiResponse = new APIViewData(1, $deployment, "Unable to detect contact groups specified: {$contactgroup}"); $app->halt(404, $apiResponse->returnJson()); } else { $contactGroupsInfo = RevDeploy::getDeploymentContactGroup($deployment, $contactgroup, $deployRev); $apiResponse = new APIViewData(0, $deployment, false); $apiResponse->setExtraResponseData('contact_group', $contactGroupsInfo); } } $apiResponse->printJson(); })->name('saigon-api-get-contact-groups'); $app->post('/sapi/contactgroup/:deployment', function ($deployment) use($app) { check_deployment_exists($app, $deployment); check_auth($app, $deployment); check_revision_status($deployment); $request = $app->request(); $contentType = $request->headers('Content-Type'); if ($contentType == 'application/json') { $contactGroupsInfo = $request->getBody(); $contactGroupsInfo = json_decode($contactGroupsInfo, true); } elseif (preg_match("/form-(data|urlencoded)/", $contentType)) { $keys = array('name', 'alias');
*/ $app->get('/sapi/consumer/routervms/:zone', function ($zone) use($app) { $zone = strtoupper($zone); if (CDC_DS::isRouterZone($zone) === false) { $apiResponse = new APIViewData(1, false, "Unable to detect router vm zone specified: {$zone}"); $apiResponse->setExtraResponseData('zone', $zone); $app->halt(403, $apiResponse->returnJson()); } $results = json_decode(CDC_DS::getRouterInfo($zone), true); $apiResponse = new APIViewData(0, $zone, false); $apiResponse->setExtraResponseData('routervms', $results); $apiResponse->printJson(); })->name('saigon-api-consumer-routervms'); /* * Saigon Host Audit Consumer Output */ $app->get('/sapi/consumer/hostaudit/:deployment', function ($deployment) use($app) { check_deployment_exists($app, $deployment); $hostAuditInfo = array(); $hostAuditInfo = NagTester::getDeploymentHostAuditInfo($deployment); if (empty($hostAuditInfo)) { $apiResponse = new APIViewData(1, $deployment, "Unable to detect Saigon Host Audit Results"); $app->halt(403, $apiResponse->returnJson()); } $hostAuditInfo = json_decode($hostAuditInfo); $apiResponse = new APIViewData(0, $deployment, false); foreach ($hostAuditInfo as $key => $value) { $apiResponse->setExtraResponseData($key, $value); } $apiResponse->printJson(); })->name('saigon-api-consumer-hostaudit');
} if (empty($results)) { $apiResponse = new APIViewData(1, $deployment, "Unable to detect host templates specified: {$hosttemplate}"); $app->halt(404, $apiResponse->returnJson()); } else { $apiResponse = new APIViewData(0, $deployment, false); $apiResponse->setExtraResponseData('host_templates', $results); } } else { if (RevDeploy::existsDeploymentHostTemplate($deployment, $hosttemplate, $deployRev) === false) { $apiResponse = new APIViewData(1, $deployment, "Unable to detect host template specified: {$hosttemplate}"); $app->halt(404, $apiResponse->returnJson()); } else { $hostTemplateInfo = RevDeploy::getDeploymentHostTemplate($deployment, $hosttemplate, $deployRev); $apiResponse = new APIViewData(0, $deployment, false); $apiResponse->setExtraResponseData('host_template', $hostTemplateInfo); } } $apiResponse->printJson(); })->name('saigon-api-get-host-template'); $app->post('/sapi/hosttemplate/:deployment', function ($deployment) use($app) { check_deployment_exists($app, $deployment); check_auth($app, $deployment); check_revision_status($deployment); $request = $app->request(); $contentType = $request->headers('Content-Type'); if ($contentType == 'application/json') { $hostTemplateInfo = $request->getBody(); $hostTemplateInfo = json_decode($hostTemplateInfo, true); } elseif (preg_match("/form-(data|urlencoded)/", $contentType)) { $keys = array('name', 'alias', 'hostgroups', 'check_command', 'initial_state', 'max_check_attempts', 'check_interval', 'retry_interval', 'active_checks_enabled', 'passive_checks_enabled', 'check_period', 'obsess_over_host', 'check_freshness', 'freshness_threshold', 'event_handler', 'event_handler_enabled', 'low_flap_threshold', 'high_flap_threshold', 'flap_detection_enabled', 'flap_detection_options', 'process_perf_data', 'retain_status_information', 'retain_nonstatus_information', 'contacts', 'contact_groups', 'notification_interval', 'first_notification_delay', 'notification_period', 'notification_options', 'notifications_enabled', 'stalking_options', 'notes', 'notes_url', 'action_url', 'icon_image', 'icon_image_alt', 'vrml_image', 'statusmap_image', '2d_coords', '3d_coords', 'use', 'parents');
$results[$tmptp] = $timeperiodData; } } if (!empty($results)) { $apiResponse = new APIViewData(0, $deployment, false); $apiResponse->setExtraResponseData('timeperiods', $results); $apiResponse->printJson(); } else { $apiResponse = new APIViewData(1, $deployment, "Unable to detect timeperiod(s) specified: {$timeperiod}"); $app->halt(404, $apiResponse->returnJson()); } } else { $timeperiodData = RevDeploy::getDeploymentTimeperiod($deployment, $timeperiod, $deployRev); if ($timeperiodData !== false) { $apiResponse = new APIViewData(0, $deployment, false); $apiResponse->setExtraResponseData('timeperiod', $timeperiodData); $apiResponse->printJson(); } else { $apiResponse = new APIViewData(1, $deployment, "Unable to detect timeperiod specified: {$timeperiod}"); $app->halt(404, $apiResponse->returnJson()); } } })->name('saigon-api-get-timeperiod'); $app->delete('/sapi/timeperiod/:deployment/:timeperiod', function ($deployment, $timeperiod) use($app) { check_deployment_exists($app, $deployment); check_auth($app, $deployment); check_revision_status($deployment); $deployRev = RevDeploy::getDeploymentNextRev($deployment); if (preg_match('/,/', $timeperiod)) { $results = array(); $tmptps = preg_split('/\\s?,\\s?/', $timeperiod);
} if (empty($results)) { $apiResponse = new APIViewData(1, $deployment, "Unable to detect host groups specified: {$hostgroup}"); $app->halt(404, $apiResponse->returnJson()); } else { $apiResponse = new APIViewData(0, $deployment, false); $apiResponse->setExtraResponseData('host_groups', $results); } } else { if (RevDeploy::existsDeploymentHostGroup($deployment, $hostgroup, $deployRev) === false) { $apiResponse = new APIViewData(1, $deployment, "Unable to detect host groups specified: {$hostgroup}"); $app->halt(404, $apiResponse->returnJson()); } else { $hostGroupsInfo = RevDeploy::getDeploymentHostGroup($deployment, $hostgroup, $deployRev); $apiResponse = new APIViewData(0, $deployment, false); $apiResponse->setExtraResponseData('host_group', $hostGroupsInfo); } } $apiResponse->printJson(); })->name('saigon-api-get-host-groups'); $app->post('/sapi/hostgroup/:deployment', function ($deployment) use($app) { check_deployment_exists($app, $deployment); check_auth($app, $deployment); check_revision_status($deployment); $request = $app->request(); $contentType = $request->headers('Content-Type'); if ($contentType == 'application/json') { $hostGroupsInfo = $request->getBody(); $hostGroupsInfo = json_decode($hostGroupsInfo, true); } elseif (preg_match("/form-(data|urlencoded)/", $contentType)) { $keys = array('name', 'alias');
if (empty($plugindata)) { $apiResponse = new APIViewData(1, $deployment, "Unable to detect specified supplemental nrpe plugin: {$plugins}"); $app->halt(403, $apiResponse->returnJson()); } unset($plugindata['deployment']); unset($plugindata['desc']); $results[$plugins] = $plugindata; echo json_encode($results); return; } })->via('GET', 'POST'); $app->map('/api/getRouterVM/:zone', function ($zone) use($app) { $zone = strtoupper($zone); if (CDC_DS::isRouterZone($zone) === false) { $apiResponse = new APIViewData(1, false, "Unable to detect router vm zone specified: {$zone}"); $apiResponse->setExtraResponseData('zone', $zone); $app->halt(403, $apiResponse->returnJson()); } $results = CDC_DS::getRouterInfo($zone); echo $results; return; })->via('GET', 'POST'); $app->map('/api/getNagiosPlugin/:deployment/:plugins', function ($deployment, $plugins) use($app) { check_deployment_exists($app, $deployment); $deployrev = RevDeploy::getDeploymentRev($deployment); if (preg_match('/,/', $plugins)) { $tmpplugins = preg_split('/,\\s?/', $plugins); $results = array(); foreach ($tmpplugins as $plugin) { $plugindata = RevDeploy::getDeploymentNagiosPlugin($deployment, $plugin, $deployrev); if (empty($plugindata)) {
$results[$cmd] = $cmdData; } } if (!empty($results)) { $apiResponse = new APIViewData(0, $deployment, false); $apiResponse->setExtraResponseData('commands', $results); $apiResponse->printJson(); } else { $apiResponse = new APIViewData(1, $deployment, "Unable to detect nrpe command(s) specified: {$nrpeCmd}"); $app->halt(404, $apiResponse->returnJson()); } } else { $cmdData = RevDeploy::getDeploymentNRPECmd($deployment, $nrpeCmd, $deployRev); if ($cmdData !== false) { $apiResponse = new APIViewData(0, $deployment, false); $apiResponse->setExtraResponseData('command', $cmdData); $apiResponse->printJson(); } else { $apiResponse = new APIViewData(1, $deployment, "Unable to detect nrpe command specified: {$nrpeCmd}"); $app->halt(404, $apiResponse->returnJson()); } } })->name('saigon-api-get-nrpe-cmd'); $app->delete('/sapi/nrpecmd/:deployment/:nrpecmd', function ($deployment, $nrpeCmd) use($app) { check_deployment_exists($app, $deployment); check_auth($app, $deployment); check_revision_status($deployment); $deployRev = RevDeploy::getDeploymentNextRev($deployment); if (preg_match('/,/', $nrpeCmd)) { $results = array(); $cmds = preg_split('/\\s?,\\s?/', $nrpeCmd);
} if (empty($results)) { $apiResponse = new APIViewData(1, $deployment, "Unable to detect service templates specified: {$servicetemplate}"); $app->halt(404, $apiResponse->returnJson()); } else { $apiResponse = new APIViewData(0, $deployment, false); $apiResponse->setExtraResponseData('service_templates', $results); } } else { if (RevDeploy::existsDeploymentSvcTemplate($deployment, $servicetemplate, $deployRev) === false) { $apiResponse = new APIViewData(1, $deployment, "Unable to detect service template specified: {$servicetemplate}"); $app->halt(404, $apiResponse->returnJson()); } else { $serviceTemplateInfo = RevDeploy::getDeploymentSvcTemplate($deployment, $servicetemplate, $deployRev); $apiResponse = new APIViewData(0, $deployment, false); $apiResponse->setExtraResponseData('service_template', $serviceTemplateInfo); } } $apiResponse->printJson(); })->name('saigon-api-get-service-template'); $app->post('/sapi/servicetemplate/:deployment', function ($deployment) use($app) { check_deployment_exists($app, $deployment); check_auth($app, $deployment); check_revision_status($deployment); $request = $app->request(); $contentType = $request->headers('Content-Type'); if ($contentType == 'application/json') { $serviceTemplateInfo = $request->getBody(); $serviceTemplateInfo = json_decode($serviceTemplateInfo, true); } elseif (preg_match("/form-(data|urlencoded)/", $contentType)) { $keys = array('name', 'alias', 'use', 'servicegroups', 'is_volatile', 'check_command', 'initial_state', 'max_check_attempts', 'check_interval', 'retry_interval', 'active_checks_enabled', 'passive_checks_enabled', 'check_period', 'obsess_over_service', 'check_freshness', 'freshness_threshold', 'event_handler', 'event_handler_enabled', 'low_flap_threshold', 'high_flap_threshold', 'flap_detection_enabled', 'flap_detection_options', 'process_perf_data', 'retain_status_information', 'retain_nonstatus_information', 'notification_interval', 'first_notification_delay', 'notification_period', 'notification_options', 'notifications_enabled', 'contacts', 'contact_groups', 'stalking_options', 'icon_image', 'icon_image_alt', 'parallelize_check');
} if (empty($results)) { $apiResponse = new APIViewData(1, $deployment, "Unable to detect service dependencies specified: {$servicedependency}"); $app->halt(404, $apiResponse->returnJson()); } else { $apiResponse = new APIViewData(0, $deployment, false); $apiResponse->setExtraResponseData('service_dependencies', $results); } } else { if (RevDeploy::existsDeploymentSvcDependency($deployment, $servicedependency, $deployRev) === false) { $apiResponse = new APIViewData(1, $deployment, "Unable to detect service dependency specified: {$servicedependency}"); $app->halt(404, $apiResponse->returnJson()); } else { $serviceDependencyInfo = RevDeploy::getDeploymentSvcDependency($deployment, $servicedependency, $deployRev); $apiResponse = new APIViewData(0, $deployment, false); $apiResponse->setExtraResponseData('service_dependency', $serviceDependencyInfo); } } $apiResponse->printJson(); })->name('saigon-api-get-service-dependency'); $app->post('/sapi/servicedependency/:deployment', function ($deployment) use($app) { check_deployment_exists($app, $deployment); check_auth($app, $deployment); check_revision_status($deployment); $request = $app->request(); $contentType = $request->headers('Content-Type'); if ($contentType == 'application/json') { $serviceDependencyInfo = $request->getBody(); $serviceDependencyInfo = json_decode($serviceDependencyInfo, true); } elseif (preg_match("/form-(data|urlencoded)/", $contentType)) { $keys = array('name', 'dependent_service_description', 'service_description', 'inherits_parent', 'execution_failure_criteria', 'notification_failure_criteria', 'dependency_period');
if ($contentType == 'application/json') { $supNRPECfgInfo = $request->getBody(); $supNRPECfgInfo = json_decode($supNRPECfgInfo, true); } elseif (preg_match("/form-(data|urlencoded)/", $contentType)) { $supNRPECfgInfo['location'] = $request->post('location'); $supNRPECfgInfo['cmds'] = $request->post('cmds'); } // A bit of param validation if (!isset($supNRPECfgInfo['location']) || empty($supNRPECfgInfo['location'])) { $apiResponse = new APIViewData(1, $deployment, "Unable to detect location parameter (unix path including filename)"); $app->halt(404, $apiResponse->returnJson()); } // Param manipulation depending on what is detected if (is_array($supNRPECfgInfo['cmds'])) { $supNRPECfgInfo['cmds'] = implode(',', $supNRPECfgInfo['cmds']); } check_revision_status($deployment); $deployRev = RevDeploy::getDeploymentNextRev($deployment); if (RevDeploy::existsDeploymentSupNRPECfg($deployment, $deployRev) === true) { RevDeploy::modifyDeploymentSupNRPECfg($deployment, $supNRPECfgInfo, $deployRev); $msg = "Successfully Modified NRPE Config"; } else { RevDeploy::createDeploymentSupNRPECfg($deployment, $supNRPECfgInfo, $deployRev); $msg = "Successfully Created NRPE Config"; } $supNRPECfgInfo = RevDeploy::getDeploymentNRPECfg($deployment, $deployRev); $fileContents = NRPECreate::buildSupNRPEFile($deployment, $deployRev, $supNRPECfgInfo); $apiResponse = new APIViewData(0, $deployment, $msg); $apiResponse->setExtraResponseData('md5', md5($fileContents)); $apiResponse->printJson(); })->name('saigon-api-create-sup-nrpe-cfg');
} if (empty($results)) { $apiResponse = new APIViewData(1, $deployment, "Unable to detect service escalations specified: {$serviceescalation}"); $app->halt(404, $apiResponse->returnJson()); } else { $apiResponse = new APIViewData(0, $deployment, false); $apiResponse->setExtraResponseData('service_escalations', $results); } } else { if (RevDeploy::existsDeploymentSvcEscalation($deployment, $serviceescalation, $deployRev) === false) { $apiResponse = new APIViewData(1, $deployment, "Unable to detect service escalation specified: {$serviceescalation}"); $app->halt(404, $apiResponse->returnJson()); } else { $serviceEscalationInfo = RevDeploy::getDeploymentSvcEscalation($deployment, $serviceescalation, $deployRev); $apiResponse = new APIViewData(0, $deployment, false); $apiResponse->setExtraResponseData('service_escalation', $serviceEscalationInfo); } } $apiResponse->printJson(); })->name('saigon-api-get-service-escalation'); $app->post('/sapi/serviceescalation/:deployment', function ($deployment) use($app) { check_deployment_exists($app, $deployment); check_auth($app, $deployment); check_revision_status($deployment); $request = $app->request(); $contentType = $request->headers('Content-Type'); if ($contentType == 'application/json') { $serviceEscalationInfo = $request->getBody(); $serviceEscalationInfo = json_decode($serviceEscalationInfo, true); } elseif (preg_match("/form-(data|urlencoded)/", $contentType)) { $keys = array('name', 'service_description', 'first_notification', 'last_notification', 'notification_interval', 'escalation_period', 'escalation_options', 'contacts', 'contact_groups');
function validateEmail($app, $deployment, $key, $value) { if (filter_var($value, FILTER_VALIDATE_EMAIL) === false) { $apiResponse = new APIViewData(1, $deployment, "Unable to use parameter value specified, value doesn't seem to be a valid email"); $apiResponse->setExtraResponseData('parameter', $key); $apiResponse->setExtraResponseData('parameter-value', $value); $app->halt(404, $apiResponse->returnJson()); } return true; }
// Do Nothing } else { $nodeTemplateInfo['services'] = array($nodeTemplateInfo['services']); } if (isset($nodeTemplateInfo['nservices']) && !empty($nodeTemplateInfo['nservices']) && preg_match('/,/', $nodeTemplateInfo['nservices'])) { $nodeTemplateInfo['nservices'] = preg_split('/\\s?,\\s?/', $nodeTemplateInfo['nservices']); } elseif (empty($nodeTemplateInfo['nservices'])) { // Do Nothing } else { $nodeTemplateInfo['nservices'] = array($nodeTemplateInfo['nservices']); } } else { $nodeTemplateInfo = RevDeploy::getDeploymentNodeTemplatewInfo($deployment, $template, $deployRev, true); } $apiResponse = new APIViewData(0, $deployment, false); $apiResponse->setExtraResponseData('template', $nodeTemplateInfo); } } $apiResponse->printJson(); })->name('saigon-api-get-dynamic-template'); $app->post('/sapi/matrix/:deployment/dynamic', function ($deployment) use($app) { check_deployment_exists($app, $deployment); check_auth($app, $deployment); $request = $app->request(); $contentType = $request->headers('Content-Type'); if ($contentType == 'application/json') { $templateInfo = $request->getBody(); $templateInfo = json_decode($templateInfo, true); } elseif (preg_match("/form-(data|urlencoded)/", $contentType)) { $templateInfo = array(); $templateInfo['name'] = $request->post('name');