function timeperiodBuildTimes($app, $deployment, array $timeperiodInfo, $use_enabled = false) { if ($use_enabled === false) { if (!isset($timeperiodInfo['timeperiods']) || empty($timeperiodInfo['timeperiods'])) { $apiResponse = new APIViewData(1, $deployment, "Unable to detect timeperiods parameter (expected array of hashes [{directive=>value,range=>value},..] )"); $app->halt(404, $apiResponse->returnJson()); } elseif (!is_array($timeperiodInfo['timeperiods'])) { $apiResponse = new APIViewData(1, $deployment, "Unable to use timeperiods parameter (expected array of hashes [{directive=>value,range=>value},..] )"); $app->halt(404, $apiResponse->returnJson()); } } else { if (!isset($timeperiodInfo['timeperiods']) || empty($timeperiodInfo['timeperiods'])) { return array(); } elseif (!is_array($timeperiodInfo['timeperiods'])) { // Not ideal, but I'll think of some way to deal with this... return array(); } } $results = array(); foreach ($timeperiodInfo['timeperiods'] as $key => $dArray) { if (!isset($dArray['directive']) || empty($dArray['directive'])) { continue; } elseif (!isset($dArray['range']) || empty($dArray['range'])) { continue; } $results[md5($dArray['directive'])] = $dArray; } return $results; }
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; }
function validateContacts($app, $deployment, array $info) { $contacts = false; $contact_groups = false; if (isset($info['contacts']) && !empty($info['contacts'])) { $contacts = true; } if (isset($info['contact_groups']) && !empty($info['contact_groups'])) { $contact_groups = true; } if ($contacts === true || $contact_groups === true) { return true; } $apiResponse = new APIViewData(1, $deployment, "Unable to detect either contacts or contact_group parameter"); $app->halt(404, $apiResponse->returnJson()); }
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');
$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); foreach ($plugins as $plugin) { RevDeploy::deleteDeploymentNagiosPlugin($deployment, $plugin, $deployRev); } } else { RevDeploy::deleteDeploymentNagiosPlugin($deployment, $nagiosPlugin, $deployRev); } $apiResponse = new APIViewData(0, $deployment, "Successfully Removed Nagios Plugin(s): {$nagiosPlugin}"); $apiResponse->printJson(); })->name('saigon-api-delete-nagios-plugin');
} else { $plugindata = RevDeploy::getDeploymentNagiosPlugin($deployment, $plugins, $deployrev); if (empty($plugindata)) { $commonrev = RevDeploy::getDeploymentRev('common'); $plugindata = RevDeploy::getDeploymentNagiosPlugin('common', $plugins, $commonrev); if (empty($plugindata)) { $apiResponse = new APIViewData(1, $deployment, "Unable to detect specified nagios 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/getNagiosPlugins/:deployment', function ($deployment) use($app) { check_deployment_exists($app, $deployment); $deployrev = RevDeploy::getDeploymentRev($deployment); $plugindata = RevDeploy::getCommonMergedDeploymentNagiosPluginsMetaData($deployment, $deployrev); if (empty($plugindata)) { $apiResponse = new APIViewData(1, $deployment, "Unable to fetch plugins for {$deployment}"); $app->halt(403, $apiResponse->returnJson()); } foreach ($plugindata as $plugin => $pArray) { unset($plugindata[$plugin]['deployment']); } echo json_encode($plugindata); return; })->via('GET', 'POST');
*/ $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');