New Paste

 

Recent Pastes

Administrate




Pastebin on p.luis.im

PasteID: 605
Pasted by Anonymous, 2019-06-22 11:40:46 GMT
Expires Never
Paste size 10.87 Kb
Tools Raw   Download
 
  1. <?php 
  2. /* 
  3.  +-------------------------------------------------------------------------+ 
  4.  | Copyright (C) 2004-2019 The Cacti Group                                 | 
  5.  |                                                                         | 
  6.  | This program is free software; you can redistribute it and/or           | 
  7.  | modify it under the terms of the GNU General Public License             | 
  8.  | as published by the Free Software Foundation; either version 2          | 
  9.  | of the License, or (at your option) any later version.                  | 
  10.  |                                                                         | 
  11.  | This program is distributed in the hope that it will be useful,         | 
  12.  | but WITHOUT ANY WARRANTY; without even the implied warranty of          | 
  13.  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           | 
  14.  | GNU General Public License for more details.                            | 
  15.  +-------------------------------------------------------------------------+ 
  16.  | Cacti: The Complete RRDtool-based Graphing Solution                     | 
  17.  +-------------------------------------------------------------------------+ 
  18.  | This code is designed, written, and maintained by the Cacti Group. See  | 
  19.  | about.php and/or the AUTHORS file for specific developer information.   | 
  20.  +-------------------------------------------------------------------------+ 
  21.  | http://www.cacti.net/                                                   | 
  22.  +-------------------------------------------------------------------------+ 
  23. */ 
  24.  
  25. require(__DIR__ . '/include/cli_check.php'); 
  26.  
  27. declare(ticks = 1); 
  28.  
  29. /* need to capture signals from users */ 
  30. function sig_handler($signo) { 
  31. 	global $include_file, $function, $parameters; 
  32.  
  33. 	switch ($signo) { 
  34. 		case SIGTERM: 
  35. 		case SIGINT: 
  36. 		case SIGABRT: 
  37. 		case SIGQUIT: 
  38. 		case SIGSEGV: 
  39. 			cacti_log("WARNING: Script Server terminated with signal '$signo' in file:'" . basename($include_file) . "', function:'$function', params:'$parameters'", false, 'PHPSVR'); 
  40. 			db_close(); 
  41.  
  42. 			exit; 
  43. 			break; 
  44. 		default: 
  45. 			cacti_log("WARNING: Script Server received signal '$signo' in file:'$include_file', function:'$function', params:'$parameters'", false, 'PHPSVR'); 
  46.  
  47. 			break; 
  48.  
  49. /* used for includes */ 
  50. /* install signal handlers for UNIX only */ 
  51. if (function_exists('pcntl_signal')) { 
  52. 	pcntl_signal(SIGTERM, 'sig_handler'); 
  53. 	pcntl_signal(SIGINT, 'sig_handler'); 
  54. 	pcntl_signal(SIGABRT, 'sig_handler'); 
  55. 	pcntl_signal(SIGQUIT, 'sig_handler'); 
  56. 	pcntl_signal(SIGSEGV, 'sig_handler'); 
  57.  
  58. error_reporting(0); 
  59.  
  60. /* define STDOUT/STDIN file descriptors if not running under CLI */ 
  61. if (php_sapi_name() != 'cli') { 
  62. 	define('STDIN', fopen('php://stdin', 'r')); 
  63. 	define('STDOUT', fopen('php://stdout', 'w')); 
  64.  
  65. /* signal for realtime */ 
  66. global $environ, $poller_id; 
  67.  
  68. /* record the script start time */ 
  69. $start = microtime(true); 
  70.  
  71. /* some debugging */ 
  72. $pid = getmypid(); 
  73. $ctr = 0; 
  74.  
  75. /* if multiple polling intervals are defined, compensate for them */ 
  76. $polling_interval = read_config_option('poller_interval'); 
  77.  
  78. if (!empty($polling_interval)) { 
  79. 	$num_polling_items = db_fetch_cell_prepared('SELECT count(*) FROM poller_item WHERE rrd_next_step <= 0 AND poller_id = ?', array($config['poller_id']), 'count(*)'); 
  80. 	define('MAX_POLLER_RUNTIME', $polling_interval); 
  81. } else { 
  82. 	$num_polling_items = db_fetch_cell_prepared('SELECT count(*) FROM poller_item WHERE poller_id = ?', array($config['poller_id']), 'count(*)'); 
  83. 	define('MAX_POLLER_RUNTIME', 300); 
  84.  
  85. /* Let PHP only run 1 second longer than the max runtime */ 
  86. ini_set('max_execution_time', MAX_POLLER_RUNTIME + 1); 
  87.  
  88. /* Record the calling environment */ 
  89. if ($_SERVER['argc'] >= 2) { 
  90. 	if ($_SERVER['argv'][1] == 'spine') 
  91. 		$environ = 'spine'; 
  92. 	else 
  93. 		if (($_SERVER['argv'][1] == 'cmd.php') || ($_SERVER['argv'][1] == 'cmd')) 
  94. 			$environ = 'cmd'; 
  95. 		elseif ($_SERVER['argv'][1] == 'realtime') 
  96. 			$environ = 'realtime'; 
  97. 		else 
  98. 			$environ = 'other'; 
  99.  
  100. 	if ($_SERVER['argc'] == 3) 
  101. 		$poller_id = $_SERVER['argv'][2]; 
  102. 	else 
  103. 		$poller_id = 1; 
  104. } else { 
  105. 	$environ = 'cmd'; 
  106. 	$poller_id = 1; 
  107.  
  108. cacti_log('DEBUG: SERVER: ' . $environ, false, 'PHPSVR', POLLER_VERBOSITY_DEBUG); 
  109.  
  110. if ($config['cacti_server_os'] == 'win32') { 
  111. 	cacti_log('DEBUG: GETCWD: ' . strtolower(strtr(getcwd(),"\\",'/')), false, 'PHPSVR', POLLER_VERBOSITY_DEBUG); 
  112. 	cacti_log('DEBUG: DIRNAM: ' . strtolower(strtr(dirname(__FILE__),"\\",'/')), false, 'PHPSVR', POLLER_VERBOSITY_DEBUG); 
  113. } else { 
  114. 	cacti_log('DEBUG: GETCWD: ' . strtr(getcwd(),"\\",'/'), false, 'PHPSVR', POLLER_VERBOSITY_DEBUG); 
  115. 	cacti_log('DEBUG: DIRNAM: ' . strtr(dirname(__FILE__),"\\",'/'), false, 'PHPSVR', POLLER_VERBOSITY_DEBUG); 
  116.  
  117. cacti_log('DEBUG: FILENM: ' . __FILE__, false, 'PHPSVR', POLLER_VERBOSITY_DEBUG); 
  118.  
  119. /* send status back to the server */ 
  120. cacti_log('PHP Script Server has Started - Parent is ' . $environ, false, 'PHPSVR', POLLER_VERBOSITY_HIGH); 
  121.  
  122. fputs(STDOUT, 'PHP Script Server has Started - Parent is ' . $environ . "\n"); 
  123. fflush(STDOUT); 
  124.  
  125. /* process waits for input and then calls functions as required */ 
  126. while (1) { 
  127. 	$result = ''; 
  128.  
  129. 	$input_string    = fgets(STDIN, 1024); 
  130. 	$function        = ''; 
  131. 	$parameters      = ''; 
  132. 	$parameter_array = array(); 
  133.  
  134. 	if ($input_string !== false) { 
  135. 		$input_string = trim($input_string); 
  136.  
  137. 		if (substr($input_string,0,4) == 'quit') { 
  138. 			fputs(STDOUT, 'PHP Script Server Shutdown request received, exiting' . PHP_EOL); 
  139. 			fflush(STDOUT); 
  140. 			cacti_log('DEBUG: PHP Script Server Shutdown request received, exiting', false, 'PHPSVR', POLLER_VERBOSITY_DEBUG); 
  141. 			db_close(); 
  142. 			exit(0); 
  143.  
  144. 		if ($input_string != '') { 
  145. 			/* pull off the parameters */ 
  146. 			$i = 0; 
  147. 			while ( true ) { 
  148. 				$pos = strpos($input_string, ' '); 
  149.  
  150. 				if ($pos > 0) { 
  151. 					switch ($i) { 
  152. 					case 0: 
  153. 						/* cut off include file as first part of input string and keep rest for further parsing */ 
  154. 						$include_file = trim(substr($input_string,0,$pos)); 
  155. 						$input_string = trim(strchr($input_string, ' ')) . ' '; 
  156. 						break; 
  157. 					case 1: 
  158. 						/* cut off function as second part of input string and keep rest for further parsing */ 
  159. 						$function = trim(substr($input_string,0,$pos), "' "); 
  160. 						$input_string = trim(strchr($input_string, ' ')) . ' '; 
  161. 						break; 
  162. 					case 2: 
  163. 						/* take the rest as parameter(s) to the function stripped off previously */ 
  164. 						$parameters = trim($input_string); 
  165. 						break 2; 
  166. 				} else { 
  167. 					break; 
  168.  
  169. 				$i++; 
  170.  
  171. 			if (!parseArgs($parameters, $parameter_array)) { 
  172. 				cacti_log("WARNING: Script Server count not parse '$parameters' for $function", false, 'PHPSVR'); 
  173. 				fputs(STDOUT, "U\n"); 
  174. 				fflush(STDOUT); 
  175. 				continue; 
  176.  
  177. 			cacti_log("DEBUG: PID[$pid] CTR[$ctr] INC: '". basename($include_file) . 
  178. 			        "' FUNC: '$function' PARMS: '" . implode('\', \'',$parameter_array) . 
  179. 			        "'", false, 'PHPSVR', POLLER_VERBOSITY_DEBUG); 
  180.  
  181. 			/* validate the existance of the function, and include if applicable */ 
  182. 			if (!function_exists($function)) { 
  183. 				if (file_exists($include_file)) { 
  184. 					/* quirk in php on Windows, believe it or not.... */ 
  185. 					/* path must be lower case */ 
  186. 					if ($config['cacti_server_os'] == 'win32') { 
  187. 						$include_file = strtolower($include_file); 
  188.  
  189. 					/* set this variable so the calling script can determine if it was called 
  190. 					 * by the script server or stand-alone */ 
  191. 					$called_by_script_server = true; 
  192.  
  193. 					/* turn on output buffering to avoid problems with nasty scripts */ 
  194. 					ob_start(); 
  195. 					include_once($include_file); 
  196. 					ob_end_clean(); 
  197. 				} else { 
  198. 					cacti_log('WARNING: PHP Script File to be included, does not exist', false, 'PHPSVR'); 
  199.  
  200. 			if (function_exists($function)) { 
  201. 				if ($parameters == '') { 
  202. 					$result = call_user_func($function); 
  203. 				} else { 
  204. 					$result = call_user_func_array($function, $parameter_array); 
  205.  
  206. 				fputs(STDOUT, trim($result) . "\n"); 
  207. 				fflush(STDOUT); 
  208.  
  209. 				cacti_log("DEBUG: PID[$pid] CTR[$ctr] RESPONSE:'$result'", false, 'PHPSVR', POLLER_VERBOSITY_DEBUG); 
  210.  
  211. 				$ctr++; 
  212. 			} else { 
  213. 				cacti_log("WARNING: Function does not exist  INC: '". basename($include_file) . "' FUNC: '" .$function . "' PARMS: '" . $parameters . "'", false, 'PHPSVR'); 
  214. 				fputs(STDOUT, "U\n"); 
  215. 				fflush(STDOUT); 
  216. 	} else { 
  217. 		cacti_log('ERROR: Input Expected, Script Server Terminating', false, 'PHPSVR'); 
  218. 		fputs(STDOUT, "ERROR: Input Expected, Script Server Terminating\n"); 
  219. 		fflush(STDOUT); 
  220. 		exit (-1); 
  221.  
  222. 	/* end the process if the runtime exceeds MAX_POLLER_RUNTIME */ 
  223. 	if (($start + MAX_POLLER_RUNTIME) < time()) { 
  224. 		cacti_log('Maximum runtime of ' . MAX_POLLER_RUNTIME . ' seconds exceeded for the Script Server. Exiting.', true, 'PHPSVR'); 
  225. 		exit (-1); 
  226.  
  227. function parseArgs($string, &$str_list, $debug = false) { 
  228. 	$delimiters = array("'",'"'); 
  229. 	$delimited  = false; 
  230. 	$str_list   = array(); 
  231.  
  232. 	if ($debug) echo "String: '" . $string . "'\n"; 
  233.  
  234. 	foreach($delimiters as $delimiter) { 
  235. 		if (strpos($string, $delimiter) !== false) { 
  236. 			$delimited = true; 
  237. 			break; 
  238.  
  239. 	/* process the simple case */ 
  240. 	if (!$delimited) { 
  241. 		$str_list = explode(' ', $string); 
  242.  
  243. 		if ($debug) echo "Output: '" . implode(",", $str_list) . "'\n"; 
  244.  
  245. 		return true; 
  246.  
  247. 	/* Break str down into an array of characters and process */ 
  248. 	$char_array = str_split($string); 
  249. 	$escaping = false; 
  250. 	$indelim  = false; 
  251. 	$parse_ok = true; 
  252. 	$curstr   = ''; 
  253. 	foreach($char_array as $char) { 
  254. 		switch ($char) { 
  255. 		case '\'': 
  256. 		case '"': 
  257. 			if (!$indelim) { 
  258. 				if (!$escaping) { 
  259. 					$indelim = true; 
  260. 				} else { 
  261. 					$curstr .= $char; 
  262. 					$escaping = false; 
  263. 			} elseif (!$escaping) { 
  264. 				$str_list[] = $curstr; 
  265. 				$curstr     = ''; 
  266. 				$indelim    = false; 
  267. 			} elseif ($escaping) { 
  268. 				$curstr  .= $char; 
  269. 				$escaping = false; 
  270.  
  271. 			break; 
  272. 		case '\\': 
  273. 			if ($escaping) { 
  274. 				$curstr  .= $char; 
  275. 				$escaping = false; 
  276. 			} else { 
  277. 				$escaping = true; 
  278.  
  279. 			break; 
  280. 		case ' ': 
  281. 			if ($escaping) { 
  282. 				$parse_ok = false; 
  283. 				$msg = 'Parse error attempting to parse string'; 
  284. 			} elseif ($indelim) { 
  285. 				$curstr .= $char; 
  286. 			} elseif ($curstr != '') { 
  287. 				$str_list[] = $curstr; 
  288. 				$curstr = ''; 
  289.  
  290. 			break; 
  291. 		case '`': 
  292. 			$parse_ok = false; 
  293. 			$msg   = 'Backtic (`) characters not allowed'; 
  294.  
  295. 			break; 
  296. 		default: 
  297. 			if ($escaping) { 
  298. 				$parse_ok = false; 
  299. 				$msg   = 'Parse error attempting to parse string'; 
  300. 			} else { 
  301. 				$curstr .= $char; 
  302. 			break; 
  303.  
  304. 		if (!$parse_ok) { 
  305. 			break; 
  306.  
  307. 	/* Add the last str to the string array */ 
  308. 	if ($indelim || $escaping) { 
  309. 		$parse_ok = false; 
  310. 		$msg = 'Parse error attempting to parse string'; 
  311.  
  312. 	if (!$parse_ok) { 
  313. 		echo 'ERROR: ' . $msg . " '" . $string . "'\n"; 
  314. 	} elseif ($curstr != '') { 
  315. 		$str_list[] = $curstr; 
  316.  
  317. 	if ($debug) echo "Output: '" . implode(",", $str_list) . "'\n"; 
  318.  
  319. 	return $parse_ok; 
  320.  
 
 
 
 
Written by Xan Manning, 2010.