1#!/usr/bin/php 2<?php 3/////////////////////////////////////////////////////////////////////////////// 4// 5// NagiosQL 6// 7/////////////////////////////////////////////////////////////////////////////// 8// 9// (c) 2005-2020 by Martin Willisegger 10// 11// Project : NagiosQL 12// Component : Configuration scripting interface 13// Website : https://sourceforge.net/projects/nagiosql/ 14// Version : 3.4.1 15// GIT Repo : https://gitlab.com/wizonet/NagiosQL 16// 17/////////////////////////////////////////////////////////////////////////////// 18// 19// To enable scripting functionality - comment out the line below 20// ============================================================== 21exit; 22// 23// Include preprocessing file 24// ========================== 25$preAccess = 0; 26$preNoMain = 1; 27require str_replace('scripts', '', __DIR__) . 'functions/prepend_scripting.php'; 28// 29// Process post parameters 30// ======================= 31$argFunction = isset($argv[1]) ? htmlspecialchars($argv[1], ENT_QUOTES, 'utf-8') : 'none'; 32$argDomain = isset($argv[2]) ? htmlspecialchars($argv[2], ENT_QUOTES, 'utf-8') : 'none'; 33$argObject = isset($argv[3]) ? htmlspecialchars($argv[3], ENT_QUOTES, 'utf-8') : 'none'; 34if ((($argFunction == 'none') || ($argDomain == 'none')) || (($argFunction == 'write') && ($argObject == 'none')) || 35 (($argFunction != 'write') && ($argFunction != 'check') && ($argFunction != 'restart') && 36 ($argFunction != 'import'))) { 37 echo 'Usage: ' .htmlspecialchars($argv[0], ENT_QUOTES, 'utf-8')." function domain [object]\n"; 38 echo "function = write/check/restart/import\n"; 39 echo "domain = domain name like 'localhost'\n"; 40 echo "object = object name, see below:\n"; 41 echo "import: object = file name like 'hostgroups.cfg' or 'localhost.cfg'\n"; 42 echo "write: object = table name like 'tbl_contact' or simplier 'contact' without 'tbl_'\n"; 43 echo "Attention: import function replaces existing data!\n"; 44 echo "Note that the new backup and configuration files becomes the UID/GID\nfrom the calling user and probably "; 45 echo "can't be deleted via web GUI anymore!\n"; 46 exit(1); 47} 48// 49// Get domain ID 50// ============= 51/** @var \functions\MysqliDbClass $myDBClass */ 52$strSQL = "SELECT `targets` FROM `tbl_datadomain` WHERE `domain`='$argDomain'"; 53$intTarget = $myDBClass->getFieldData($strSQL); 54$strSQL = "SELECT `id` FROM `tbl_datadomain` WHERE `domain`='$argDomain'"; 55$intDomain = $myDBClass->getFieldData($strSQL); 56if ($intDomain == '') { 57 echo "Domain '".$argDomain."' doesn not exist\n"; 58 exit(1); 59} 60if ($intDomain == '0') { 61 echo "Domain '".$argDomain."' cannot be used\n"; 62 exit(1); 63} 64$myDataClass->intDomainId = $intDomain; 65$myConfigClass->intDomainId = $intDomain; 66$myImportClass->intDomainId = $intDomain; 67$myConfigClass->getConfigData($intTarget, 'method', $intMethod); 68// 69// Process form variables 70// ====================== 71if ($argFunction == 'check') { 72 $myConfigClass->getConfigData($intTarget, 'binaryfile', $strBinary); 73 $myConfigClass->getConfigData($intTarget, 'basedir', $strBaseDir); 74 $myConfigClass->getConfigData($intTarget, 'nagiosbasedir', $strNagiosBaseDir); 75 $myConfigClass->getConfigData($intTarget, 'conffile', $strConffile); 76 if ($intMethod == 1) { 77 if (file_exists($strBinary) && is_executable($strBinary)) { 78 $resFile = popen($strBinary. ' -v ' .$strConffile, 'r'); 79 } else { 80 echo "Cannot find the Nagios binary or no execute permissions!\n"; 81 exit(1); 82 } 83 } elseif ($intMethod == 2) { 84 $booReturn = 0; 85 if (empty($myConfigClass->resConnectId) || !is_resource($myConfigClass->resConnectId)) { 86 $booReturn = $myConfigClass->getFTPConnection($intTarget); 87 } 88 if ($booReturn == 1) { 89 $myVisClass->processMessage($myDataClass->strErrorMessage, $strErrorMessage); 90 } else { 91 $intErrorReporting = error_reporting(); 92 error_reporting(0); 93 if (!($resFile = ftp_exec($myConfigClass->resConnectId, $strBinary.' -v '.$strConffile))) { 94 echo "Remote execution (FTP SITE EXEC) is not supported on your system!\n"; 95 error_reporting($intErrorReporting); 96 exit(1); 97 } 98 ftp_close($conn_id); 99 error_reporting($intErrorReporting); 100 } 101 } elseif ($intMethod == 3) { 102 $booReturn = 0; 103 if (empty($myConfigClass->resConnectId) || !is_resource($myConfigClass->resConnectId)) { 104 $booReturn = $myConfigClass->getSSHConnection($intTarget); 105 } 106 if ($booReturn == 1) { 107 echo 'SSH connection failure: ' .str_replace('::', "\n", $myConfigClass->strErrorMessage); 108 exit(1); 109 } 110 $intRet1 = $myConfigClass->sendSSHCommand('ls '.$strBinary, $arrRet1); 111 $intRet2 = $myConfigClass->sendSSHCommand('ls '.$strConffile, $arrRet2); 112 if (($intRet1 == 0) && ($intRet2 == 0) && is_array($arrRet1) && is_array($arrRet2)) { 113 $intRet3 = $myConfigClass->sendSSHCommand($strBinary.' -v '.$strConffile, $arrResult); 114 if ($intRet3 != 0) { 115 echo "Remote execution of nagios verify command failed (remote SSH)!\n"; 116 exit(1); 117 } 118 } else { 119 echo "Nagios binary or configuration file not found (remote SSH)!\n"; 120 exit(1); 121 } 122 } 123} 124if ($argFunction == 'restart') { 125 // Read config file 126 $myConfigClass->getConfigData($intTarget, 'commandfile', $strCommandfile); 127 $myConfigClass->getConfigData($intTarget, 'pidfile', $strPidfile); 128 // Check state nagios demon 129 clearstatcache(); 130 if ($intMethod == 1) { 131 if (file_exists($strPidfile)) { 132 if (file_exists($strCommandfile) && is_writable($strCommandfile)) { 133 $strCommandString = '[' .time(). '] RESTART_PROGRAM;' .time()."\n"; 134 $timeout = 3; 135 $old = ini_set('default_socket_timeout', $timeout); 136 $resCmdFile = fopen($strCommandfile, 'wb'); 137 ini_set('default_socket_timeout', $old); 138 stream_set_timeout($resCmdFile, $timeout); 139 stream_set_blocking($resCmdFile, 0); 140 if ($resCmdFile) { 141 fwrite($resCmdFile, $strCommandString); 142 fclose($resCmdFile); 143 echo "Restart command successfully send to Nagios\n"; 144 exit(0); 145 } 146 } 147 echo "Restart failed - Nagios command file not found or no execute permissions\n"; 148 exit(1); 149 } 150 echo "Nagios daemon is not running, cannot send restart command!\n"; 151 exit(1); 152 } 153 if ($intMethod == 2) { 154 echo "Nagios restart is not possible via FTP remote connection!\n"; 155 exit(1); 156 } 157 if ($intMethod == 3) { 158 $booReturn = 0; 159 if (empty($myConfigClass->resConnectId) || !is_resource($myConfigClass->resConnectId)) { 160 $booReturn = $myConfigClass->getSSHConnection($intTarget); 161 } 162 if ($booReturn == 1) { 163 $myVisClass->processMessage($myDataClass->strErrorMessage, $strErrorMessage); 164 } else { 165 $intRet1 = $myConfigClass->sendSSHCommand('ls '.$strBinary, $arrRet1); 166 if (($intRet1 == 0) && is_array($arrRet1)) { 167 $strCommandString = '[' .time(). '] RESTART_PROGRAM;' .time()."\n"; 168 $strCommand = 'echo "'.$strCommandString.'" >> '.$strCommandfile; 169 $intRet2 = $myConfigClass->sendSSHCommand($strCommand, $arrResult); 170 if ($intRet2 != 0) { 171 echo "Restart failed - Nagios command file not found or no rights to execute (remote SSH)!\n"; 172 exit(1); 173 } 174 echo "Nagios daemon successfully restarted (remote SSH)\n"; 175 exit(0); 176 } 177 echo "Nagios command file not found (remote SSH)!\n"; 178 exit(1); 179 } 180 } 181} 182if ($argFunction == 'write') { 183 if (substr_count($argObject, 'tbl_') != 0) { 184 $argObject = str_replace('tbl_', '', $argObject); 185 } 186 if (substr_count($argObject, '.cfg') != 0) { 187 $argObject = str_replace('.cfg', '', $argObject); 188 } 189 if ($argObject == 'host') { 190 // Write host configuration 191 $strInfo = "Write host configurations ...\n"; 192 $strSQL = "SELECT `id` FROM `tbl_host` WHERE `config_id` = $intDomain AND `active`='1'"; 193 $myDBClass->hasDataArray($strSQL, $arrData, $intDataCount); 194 $intError = 0; 195 if ($intDataCount != 0) { 196 foreach ($arrData as $data) { 197 $intReturn = $myConfigClass->createConfigSingle('tbl_host', $data['id']); 198 if ($intReturn == 1) { 199 $intError++; 200 } 201 } 202 } 203 if ($intError == 0) { 204 $strInfo .= "Host configuration files successfully written!\n"; 205 } else { 206 $strInfo .= "Cannot open/overwrite the configuration file (check the permissions)!\n"; 207 } 208 } elseif ($argObject == 'service') { 209 // Write service configuration 210 $strInfo = "Write service configurations ...\n"; 211 $strSQL = 'SELECT `id`, `config_name` FROM `tbl_service` ' 212 . "WHERE `config_id` = $intDomain AND `active`='1' GROUP BY `config_name`"; 213 $myDBClass->hasDataArray($strSQL, $arrData, $intDataCount); 214 $intError = 0; 215 if ($intDataCount != 0) { 216 foreach ($arrData as $data) { 217 $intReturn = $myConfigClass->createConfigSingle('tbl_service', $data['id']); 218 if ($intReturn == 1) { 219 $intError++; 220 } 221 } 222 } 223 if ($intError == 0) { 224 $strInfo .= "Service configuration file successfully written!\n"; 225 } else { 226 $strInfo .= "Cannot open/overwrite the configuration file (check the permissions)!\n"; 227 } 228 } else { 229 $strInfo = 'Write ' .$argObject.".cfg ...\n"; 230 $booReturn = $myConfigClass->createConfig('tbl_' .$argObject); 231 if ($booReturn == 0) { 232 $strInfo .= 'Configuration file ' .$argObject.".cfg successfully written!\n"; 233 } else { 234 echo $myConfigClass->strErrorMessage; 235 $strInfo .= 'Cannot open/overwrite the configuration file ' .$argObject. '.cfg (check the permissions or ' 236 . 'probably tbl_' .$argObject." does not exists)!\n"; 237 } 238 } 239 echo $strInfo; 240} 241if ($argFunction == 'import') { 242 $strInfo = "Importing configurations ...\n"; 243 $intReturn = $myImportClass->fileImport($argObject, $intTarget, '1'); 244 if ($intReturn != 0) { 245 $strInfo .= $myImportClass->strErrorMessage; 246 } else { 247 $strInfo .= $myImportClass->strInfoMessage; 248 } 249 $strInfo = strip_tags($strInfo); 250 echo str_replace('::', "\n", $strInfo); 251} 252 253// 254// Output processing 255// ================= 256if (isset($resFile) && ($resFile != false)) { 257 $intError = 0; 258 $intWarning = 0; 259 $strOutput = ''; 260 while (!feof($resFile)) { 261 $strLine = fgets($resFile, 1024); 262 if (substr_count($strLine, 'Error:') != 0) { 263 $intError++; 264 } 265 if (substr_count($strLine, 'Warning:') != 0) { 266 $intWarning++; 267 } 268 $strOutput .= $strLine; 269 } 270 pclose($resFile); 271 echo $strOutput."\n"; 272 if (($intError == 0) && ($intWarning == 0)) { 273 echo "Written configuration files are valid, Nagios can be restarted!\n\n"; 274 } 275} elseif (isset($arrResult) && is_array($arrResult)) { 276 $intError = 0; 277 $intWarning = 0; 278 $strOutput = ''; 279 foreach ($arrResult as $elem) { 280 if (substr_count($elem, 'Error:') != 0) { 281 $intError++; 282 } 283 if (substr_count($elem, 'Warning:') != 0) { 284 $intWarning++; 285 } 286 $strOutput .= $elem."\n"; 287 } 288 echo $strOutput."\n"; 289 if (($intError == 0) && ($intWarning == 0)) { 290 echo "Written configuration files are valid, Nagios can be restarted!\n\n"; 291 } 292} 293