Example #1
0
 /**
  * __construct - object initialization routine
  *
  * @param mixed $deployment deployment we are making the change too
  *
  * @access public
  * @return void
  */
 public function __construct($deployment)
 {
     $amodule = AUTH_MODULE;
     $this->user = $amodule::getUser();
     $this->ip = NagMisc::getIP();
     $this->deployment = $deployment;
 }
Example #2
0
 /**
  * _fetchData - core fetch data function for NagRequest class
  * 
  * @param mixed $url url we are wanting to fetch and return
  *
  * @static
  * @access private
  * @return void
  */
 private static function _fetchData($url)
 {
     $interface = NagMisc::getInterface();
     /* Initialize Curl and Issue Request */
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_USERAGENT, 'Saigon Nagios Data Fetcher/' . VERSION . "/{$interface}");
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
     curl_setopt($ch, CURLOPT_TIMEOUT, 45);
     /* Response or No Response ? */
     $result = curl_exec($ch);
     if (curl_errno($ch)) {
         $result = curl_error($ch);
         curl_close($ch);
     } else {
         curl_close($ch);
     }
     return $result;
 }
Example #3
0
 public static function delDeploymentStaticHost($deployment, $ip)
 {
     if (self::$init === false) {
         self::init();
     }
     $hostInfo = array();
     if (preg_match('/^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$/', $ip)) {
         $ip = NagMisc::encodeIP($ip);
     }
     $staticHosts = NagRedis::get(md5('deployment:' . $deployment) . ':statichosts');
     $staticHosts = json_decode($staticHosts, true);
     if (isset($staticHosts[$ip]) && !empty($staticHosts[$ip])) {
         $hostInfo = $staticHosts[$ip];
         unset($staticHosts[$ip]);
         NagRedis::set(md5('deployment:' . $deployment) . ':statichosts', json_encode($staticHosts));
         $hostData = new DeploymentHostData($deployment, 'del', 'static', $hostInfo);
         self::$log->addToLog($hostData);
     }
     return $hostInfo;
 }
 private static function singleCurl($host, $type, $url)
 {
     $interface = NagMisc::getInterface();
     $fullUrl = "http://" . $host . $url;
     $type = strtoupper($type);
     /* Initialize Curl and Issue Request */
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: ' . VARNISH_CACHE_HOSTNAME));
     curl_setopt($ch, CURLOPT_NOBODY, true);
     curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $type);
     curl_setopt($ch, CURLOPT_URL, $fullUrl);
     curl_setopt($ch, CURLOPT_USERAGENT, 'Saigon Cache Invalidator/' . VERSION . "/{$interface}");
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_TIMEOUT, 10);
     /* Response or No Response ? */
     $result = curl_exec($ch);
     if (curl_errno($ch)) {
         $result = curl_error($ch);
         curl_close($ch);
     } else {
         curl_close($ch);
     }
     return true;
 }
Example #5
0
 public static function buildDeployment($deployment, $revision = false, $diff = false, $forcebuild = false, $shardposition = false)
 {
     self::checkPerms($deployment);
     self::$deployment = $deployment;
     self::$m_logger = new NagLogger();
     self::deleteDeployment();
     if ($revision === false) {
         /* 
             No revision means we are dealing with a request from a consumer
             so we will fetch Deployment information from Nagios Configurator API
         */
         $response = NagRequest::fetchDeploymentData($deployment, self::getSubDeployment());
         $responseObj = json_decode($response);
         $helper = new NagHelpers();
         $helper->setAliasTemplate($responseObj->miscsettings->aliastemplate);
         $helper->setGlobalNegate($responseObj->miscsettings->deploynegate);
         if (SHARDING === true && $responseObj->miscsettings->ensharding == 'on') {
             $helper->enableSharding($responseObj->miscsettings->shardkey, $responseObj->miscsettings->shardcount, SHARDING_POSITION);
         }
     } else {
         /* 
             A revision means we are dealing with a request from the UI
             so we will fetch Deployment information from Redis store directly
         */
         $response = RevDeploy::getDeploymentData($deployment, $revision, true);
         $responseObj = json_decode($response);
         $helper = new NagHelpers(true);
         $helper->setAliasTemplate($responseObj->miscsettings->aliastemplate);
         $helper->setGlobalNegate($responseObj->miscsettings->deploynegate);
         if ($shardposition !== false) {
             $deploymentInfo = RevDeploy::getDeploymentInfo($deployment);
             $helper->enableSharding($deploymentInfo['shardkey'], $deploymentInfo['shardcount'], $shardposition);
         }
     }
     /* Create Initial Files for Nagios */
     if (!empty($responseObj->timeperiods)) {
         self::createTimeperiodFile($responseObj->timeperiods);
     }
     if (!empty($responseObj->commands)) {
         self::createCommandFile($responseObj->commands);
     }
     if (!empty($responseObj->contacttemplates)) {
         self::createContactTemplateFile($responseObj->contacttemplates);
     }
     if (!empty($responseObj->contacts)) {
         self::createContactFile($responseObj->contacts);
     }
     if (!empty($responseObj->contactgroups)) {
         self::createContactGroupFile($responseObj->contactgroups);
     }
     if (!empty($responseObj->hosttemplates)) {
         self::createHostTemplateFile($responseObj->hosttemplates);
     }
     if (!empty($responseObj->hostgroups)) {
         self::createHostGroupFile($responseObj->hostgroups);
     }
     if (!empty($responseObj->servicetemplates)) {
         self::createServiceTemplateFile($responseObj->servicetemplates);
     }
     if (!empty($responseObj->servicegroups)) {
         self::createServiceGroupFile($responseObj->servicegroups);
     }
     if (!empty($responseObj->resourcecfg)) {
         self::createResourceConfigFile($responseObj->resourcecfg);
     }
     if (!empty($responseObj->cgicfg)) {
         self::createCgiConfigFile($responseObj->cgicfg);
     }
     if (!empty($responseObj->modgearmancfg)) {
         self::createModgearmanConfigFile($responseObj->modgearmancfg);
     }
     if (!empty($responseObj->nagioscfg)) {
         self::createNagiosConfigFile($responseObj->nagioscfg);
     }
     /* Initialize Helper object for storing information for hosts and services */
     if (!empty($responseObj->hostsearches)) {
         if ($diff === true && !empty(self::$m_localcache)) {
             foreach (self::$m_localcache as $module => $items) {
                 foreach ($items as $host => $hostData) {
                     $helper->importHost($host, $hostData);
                 }
             }
         } else {
             foreach ($responseObj->hostsearches as $md5Key => $hsObj) {
                 if (($subdeployment = self::getSubDeployment()) !== false) {
                     if (isset($hsObj->subdeployment) && $hsObj->subdeployment != $subdeployment) {
                         continue;
                     }
                 }
                 $module = $hsObj->location;
                 if (preg_match("/^RS-(\\w+)-(\\w+)\$/", $module)) {
                     $modObj = new RightScale();
                 } elseif (preg_match("/^AWSEC2-(\\w+)-(\\w+)\$/", $module)) {
                     $modObj = new AWSEC2();
                 } else {
                     $modObj = new $module();
                 }
                 $hostInfo = $modObj->getSearchResults($hsObj);
                 if (empty($hostInfo)) {
                     self::$m_logger->addToLog("Empty Results Detected for {$hsObj->location} : {$hsObj->srchparam}");
                     continue;
                 }
                 foreach ($hostInfo as $host => $hostData) {
                     $helper->importHost($host, $hostData);
                     if ($diff === true) {
                         if (!isset(self::$m_localcache[$module]) || !is_array(self::$m_localcache[$module])) {
                             self::$m_localcache[$module] = array();
                         }
                         self::$m_localcache[$module][$host] = $hostData;
                     }
                 }
             }
         }
     }
     if (!empty($responseObj->statichosts)) {
         foreach ($responseObj->statichosts as $encIP => $tmpObj) {
             if (($subdeployment = self::getSubDeployment()) !== false) {
                 if (isset($tmpObj->subdeployment) && $tmpObj->subdeployment != $subdeployment) {
                     continue;
                 }
             }
             $helper->importStaticHost(NagMisc::recursive_object_to_array($tmpObj));
         }
     }
     /* Import Service and Node => Service Pointer Data */
     if ($forcebuild === false) {
         if (empty($responseObj->services)) {
             return "Initial Service Data was Empty";
         } elseif (empty($responseObj->nodetemplates)) {
             return "Initial NodeTemplate Data was Empty";
         }
     }
     $helper->scrubHosts();
     if (!empty($responseObj->services)) {
         $helper->importServices($responseObj->services);
     }
     if (!empty($responseObj->nodetemplates)) {
         $helper->importNodeTemplate($responseObj->nodetemplates, self::getSubDeployment());
     }
     if (!empty($responseObj->servicedependencies)) {
         $helper->importServiceDependencies($responseObj->servicedependencies);
     }
     if (!empty($responseObj->serviceescalations)) {
         $helper->importServiceEscalations($responseObj->serviceescalations);
     }
     $hostCache = $helper->returnHosts();
     $serviceCache = $helper->returnServices($hostCache);
     $serviceDependencyCache = $helper->returnServiceDependencies($hostCache);
     $serviceEscalationsCache = $helper->returnServiceEscalations($hostCache);
     /* Create core host / service nagios config files */
     if ($forcebuild === false) {
         if (empty($hostCache)) {
             return "Host Cache is Empty, unable to create Host File";
         } elseif (empty($serviceCache)) {
             return "Service Cache is Empty, unable to create Services File";
         }
     }
     if (!empty($hostCache)) {
         self::createHostFile($hostCache);
     }
     if (!empty($serviceCache)) {
         self::createServiceFile($serviceCache);
     }
     if (!empty($serviceDependencyCache)) {
         self::createServiceDependencyFile($serviceDependencyCache);
     }
     if (!empty($serviceEscalationsCache)) {
         self::createServiceEscalationsFile($serviceEscalationsCache);
     }
     return true;
 }
Example #6
0
            $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)'));
 /**
  * add_static_host_csv - add static hosts via csv inject 
  * 
  * @access public
  * @return void
  */
 public function add_static_host_csv()
 {
     $viewData = new ViewData();
     $deployment = $this->getDeployment('deployment_error');
     $contents = $this->fetchUploadedFile('staticcsvfile');
     if ($contents === false) {
         $this->sendResponse('deployment_search_window_static', $viewData);
     }
     $lines = explode("\n", $contents);
     if (!isset($_SESSION[$deployment]['static-deployments']) || !is_array($_SESSION[$deployment]['static-deployments'])) {
         $_SESSION[$deployment]['static-deployments'] = array();
     }
     foreach ($lines as $line) {
         if (empty($line)) {
             continue;
         }
         $tmpArray = preg_split('/,\\s?/', $line);
         $encIP = NagMisc::encodeIP($tmpArray[1]);
         $_SESSION[$deployment]['static-deployments'][$encIP]['host'] = $tmpArray[0];
         $_SESSION[$deployment]['static-deployments'][$encIP]['ip'] = $tmpArray[1];
     }
     $viewData->deployment = $deployment;
     $this->sendResponse('deployment_search_window_static', $viewData);
 }