/usr/share/zabbix/include/copt.lib.php is in zabbix-frontend-php 1:1.8.11-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 | <?php
/*
** ZABBIX
** Copyright (C) 2000-2005 SIA Zabbix
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**/
/* SPEED Measurement
-= slow =-
vs
-= fast =-
1)zbx_strlen
if (zbx_strlen($foo) < 5) { echo "Foo is too short"; }
vs
if (!isset($foo{5})) { echo "Foo is too short"; }
2)++
$i++
vs
++$i
3)print
print
vs
echo
4)regexps
preg_match("![0-9]+!", $foo);
vs
ctype_digit($foo);
5)_in_array
$keys = array("apples", "oranges", "mangoes", "tomatoes", "pickles");
if (_in_array('mangoes', $keys)) { ... }
vs
$keys = array("apples" => 1, "oranges" => 1, "mangoes" => 1, "tomatoes" => 1, "pickles" => 1);
if (isset($keys['mangoes'])) { ... }
6)key_exist
if(array_key_exists('mangoes', $keys))
vs
if (isset($keys['mangoes'])) { ... }
7)regexps
POSIX-regexps
vs
Perl-regexps
8)constants
UPPER case constans (TRUE, FALSE)
vs
lower case constans (true, false)
9)for
for ($i = 0; $i < FUNCTION($j); $i++) {...}
vs
for ($i = 0, $k = FUNCTION($j); $i < $k; $i = $i + 1) {...}
10)strings
"var=$var"
vs
'var='.$var
*/
/*
** Description:
** Optimization class. Provide functions for
** PHP code optimization.
**
** Author:
** Eugene Grigorjev (eugene.grigorjev@zabbix.com)
**/
define('USE_PROFILING',1);
//define('USE_VAR_MON',1);
define('USE_TIME_PROF',1);
define('USE_MEM_PROF',1);
//define('USE_COUNTER_PROF',1);
//define('USE_MENU_PROF',1);
//define('USE_MENU_DETAILS',1);
define('USE_SQLREQUEST_PROF',1);
define('SHOW_SQLREQUEST_DETAILS',1);
// What is considered long query
define('LONG_QUERY',0.01);
// WHat is limit on total time spent on all SQL queries
define('QUERY_TOTAL_TIME',0.5);
// WHat is limit on total time spent
define('TOTAL_TIME',1.0);
//define("SHOW_SQLREQUEST_LONG_QUERIES_ONLY",1);
if(!defined('OBR')) define('OBR',"<br/>\n");
if(defined('USE_PROFILING')){
$starttime = array();
$memorystamp = array();
$sqlrequests = defined('SHOW_SQLREQUEST_DETAILS') ? array() : 0;
$sqlmark = array();
$perf_counter = array();
$var_list = array();
class COpt{
public static $memoryPick = 0;
protected static $memory_limit_reached = false;
protected static $max_memory_bytes = null;
protected static $debug_info = array();
protected static function getmicrotime() {
if(defined('USE_TIME_PROF')) {
list($usec, $sec) = explode(' ',microtime());
return ((float)$usec + (float)$sec);
}
else {
return 0;
}
}
public static function showmemoryusage($descr=null){
if(defined('USE_MEM_PROF')) {
$memory_usage = COpt::getmemoryusage();
$memory_usage = $memory_usage.'b | '.($memory_usage>>10).'K | '.($memory_usage>>20).'M';
SDI('PHP memory usage ['.$descr.'] '.$memory_usage);
}
}
public static function memoryPick(){
$memory_usage = COpt::getmemoryusage();
if($memory_usage>self::$memoryPick) self::$memoryPick = $memory_usage;
}
public static function getMemoryPick(){
if(defined('USE_MEM_PROF')) {
return self::$memoryPick;
}
else {
return 0;
}
}
public static function showMemoryPick($descr=null){
if(defined('USE_MEM_PROF')) {
$memory_usage = self::$memoryPick;
$memory_usage = $memory_usage.'b | '.($memory_usage>>10).'K | '.($memory_usage>>20).'M';
SDI('PHP memory PICK ['.$descr.'] '.$memory_usage);
}
}
protected static function getmemoryusage() {
if(defined('USE_MEM_PROF')) {
$memory_usage = memory_get_usage('memory_limit');
if($memory_usage>self::$memoryPick) self::$memoryPick = $memory_usage;
return $memory_usage;
}
else {
return 0;
}
}
public static function counter_up($type=NULL){
if(defined('USE_COUNTER_PROF')){
global $perf_counter;
global $starttime;
foreach(array_keys($starttime) as $keys){
if(!isset($perf_counter[$keys][$type]))
$perf_counter[$keys][$type]=1;
else
$perf_counter[$keys][$type]++;
}
}
}
public static function profiling_start($type=NULL){
global $starttime;
global $memorystamp;
global $sqlmark;
global $sqlrequests;
global $var_list;
global $USER_DETAILS;
if(is_null(self::$max_memory_bytes)) self::$max_memory_bytes = (ini_get('memory_limit') * 0.8 * 1024 * 1024);
if((!is_null($USER_DETAILS) && isset($USER_DETAILS['debug_mode']) && ($USER_DETAILS['debug_mode'] == GROUP_DEBUG_MODE_DISABLED))
|| self::$memory_limit_reached){
return false;
}
if(self::getmemoryusage() > self::$max_memory_bytes){
self::$memory_limit_reached = true;
}
if(is_null($type)) $type='global';
$starttime[$type] = COpt::getmicrotime();
$memorystamp[$type] = COpt::getmemoryusage();
if(defined('USE_VAR_MON')){
$var_list[$type] = isset($GLOBALS) ? array_keys($GLOBALS) : array();
}
if(defined('USE_SQLREQUEST_PROF')){
if(defined('SHOW_SQLREQUEST_DETAILS')){
$sqlmark[$type] = count($sqlrequests);
}
else{
$sqlmark[$type] = $sqlrequests;
}
}
}
public static function savesqlrequest($time, $sql){
global $USER_DETAILS;
if(is_null(self::$max_memory_bytes)) self::$max_memory_bytes = (ini_get('memory_limit') * 0.8 * 1024 * 1024);
if((!is_null($USER_DETAILS) && isset($USER_DETAILS['debug_mode']) && ($USER_DETAILS['debug_mode'] == GROUP_DEBUG_MODE_DISABLED))
|| self::$memory_limit_reached){
return false;
}
if(self::getmemoryusage() > self::$max_memory_bytes){
self::$memory_limit_reached = true;
}
if(defined('USE_SQLREQUEST_PROF')){
global $sqlrequests;
$time=round($time,6);
// echo $time,"<br>";
if(defined('SHOW_SQLREQUEST_DETAILS')){
array_push($sqlrequests, array($time,$sql));
}
else{
$sqlrequests++;
}
}
}
public static function profiling_stop($type=NULL){
global $starttime;
global $memorystamp;
global $sqlrequests;
global $sqlmark;
global $perf_counter;
global $var_list;
global $USER_DETAILS;
global $DB;
$endtime = COpt::getmicrotime();
$memory = COpt::getmemoryusage();
$pickMemory = COpt::getMemoryPick();
if(is_null($type)) $type='global';
$debug_str = '';
$debug_str.= '<a name="debug"></a>';
$debug_str.= "******************* ".S_STATS_FOR." $type *************************".OBR;
if(defined('USE_TIME_PROF')){
$time = $endtime - $starttime[$type];
if($time < TOTAL_TIME){
$debug_str.= S_TOTAL_TIME.': '.round($time,6).OBR;
}
else{
$debug_str.= '<b>'.S_TOTAL_TIME.': '.round($time,6).'</b>'.OBR;
}
}
if(defined('USE_MEM_PROF')){
$debug_str.= S_MEMORY_LIMIT.' : '.ini_get('memory_limit').OBR;
$debug_str.= S_MEMORY_USAGE.' : '.mem2str($memorystamp[$type]).' - '.mem2str($memory).
' ('.mem2str($memory - $memorystamp[$type]).')'.OBR;
$debug_str.= 'Memory peak : '.mem2str($pickMemory).OBR;
}
if(defined('USE_VAR_MON')){
$curr_var_list = isset($GLOBALS) ? array_keys($GLOBALS) : array();
$var_diff = array_diff($curr_var_list, $var_list[$type]);
$debug_str.= ' Undeleted vars : '.count($var_diff).' [';
print_r(implode(', ',$var_diff));
$debug_str.= ']'.OBR;
}
if(defined('USE_COUNTER_PROF')){
if(isset($perf_counter[$type])){
ksort($perf_counter[$type]);
foreach($perf_counter[$type] as $name => $value){
$debug_str.= 'Counter "'.$name.'" : '.$value.OBR;
}
}
}
if(defined('USE_SQLREQUEST_PROF')){
if(defined('SHOW_SQLREQUEST_DETAILS')){
$requests_cnt = count($sqlrequests);
if(isset($DB) && isset($DB['SELECT_COUNT'])){
$debug_str.= S_SQL_SELECTS_COUNT.': '.$DB['SELECT_COUNT'].OBR;
$debug_str.= S_SQL_EXECUTES_COUNT.': '.$DB['EXECUTE_COUNT'].OBR;
$debug_str.= S_SQL_REQUESTS_COUNT.': '.($requests_cnt - $sqlmark[$type]).OBR;
}
$sql_time=0;
for($i = $sqlmark[$type]; $i < $requests_cnt; $i++){
$time = $sqlrequests[$i][0];
$sqlrequests[$i][1] = str_replace(array('<','>'), array('<','>'), $sqlrequests[$i][1]);
$sql_time+=$time;
if($time < LONG_QUERY){
$debug_str.= S_TIME.':'.round($time,8).' SQL: '.$sqlrequests[$i][1].OBR;
}
else{
$debug_str.= '<b>'.S_TIME.':'.round($time,8).' LONG SQL: '.$sqlrequests[$i][1].'</b>'.OBR;
}
}
}
else{
$debug_str.= 'SQL requests count: '.($sqlrequests - $sqlmark[$type]).OBR;
}
if($sql_time < QUERY_TOTAL_TIME){
$debug_str.= S_TOTAL_TIME_SPENT_ON_SQL.': '.round($sql_time,8).OBR;
}
else{
$debug_str.= '<b>'.S_TOTAL_TIME_SPENT_ON_SQL.': '.round($sql_time,8).'</b>'.OBR;
}
}
$debug_str.= "******************** ".S_END_OF." $type ***************************".OBR;
self::$debug_info[$type] = $debug_str;
}
public static function show(){
// DEBUG of ZBX FrontEnd
$debug = new CDiv(null,'textcolorstyles');
$debug->setAttribute('name','zbx_gebug_info');
$debug->setAttribute('style','display: none; overflow: auto; width: 95%; border: 1px #777777 solid; margin: 4px; padding: 4px;');
if(self::$memory_limit_reached){
$debug->addItem(array(BR(), S_MEMORY_LIMIT_REACHED,BR()));
}
foreach(self::$debug_info as $type => $info){
$debug->addItem(array(BR(),new CJSscript($info),BR()));
}
$debug->show();
//----------------
}
public static function set_memory_limit($limit='256M'){
ini_set('memory_limit',$limit);
}
public static function compare_files_with_menu($menu=null){
if(defined('USE_MENU_PROF')){
$files_list = glob('*.php');
$result = array();
foreach($files_list as $file){
$list = array();
foreach($menu as $label=>$sub){
foreach($sub['pages'] as $sub_pages){
if(empty($sub_pages)) continue;
if(!isset($sub_pages['label'])) $sub_pages['label']=$sub_pages['url'];
$menu_path = $sub['label'].'->'.$sub_pages['label'];
if($sub_pages['url'] == $file){
array_push($list, $menu_path);
}
if(!str_in_array($sub_pages['url'], $files_list))
$result['error'][$sub_pages['url']] = array($menu_path);
if(isset($sub_pages['sub_pages'])) foreach($sub_pages['sub_pages'] as $page){
$menu_path = $sub['label'].'->'.$sub_pages['label'].'->sub_pages';
if(!str_in_array($page, $files_list))
$result['error'][$page] = array($menu_path);
if($page != $file) continue;
array_push($list, $menu_path);
}
}
}
if(count($list) != 1) $level = 'worning';
else $level = 'normal';
$result[$level][$file] = $list;
}
foreach($result as $level => $files_list){
if(defined('USE_MENU_DETAILS')){
echo OBR.'(menu check) ['.$level.OBR;
foreach($files_list as $file => $menu_list){
echo '(menu check)'.SPACE.SPACE.SPACE.SPACE.$file.' {'.implode(',',$menu_list).'}'.OBR;
}
}
else{
echo OBR.'(menu check) ['.$level.'] = '.count($files_list).OBR;
}
}
}
}
}
//COpt::set_memory_limit('256M');
COpt::profiling_start('script');
}
else{
class COpt{
static function profiling_start($type=NULL) {}
static function profiling_stop($type=NULL) {}
static function show() {}
static function savesqlrequest($sql) {}
static function showmemoryusage($descr=null) {}
static function compare_files_with_menu($menu=null) {}
static function counter_up($type=NULL) {}
}
}
?>
|