function _initialize() { #每小时执行一次 if (date('i') != 30) { exit; } $conn_db = _ocilogon($this->db); //获取V1级别的评分要求 $_row_infos = array(); $sql = "select * from {$this->report_monitor_v1} t where pinfen_rule is not null "; $stmt_list = _ociparse($conn_db, $sql); $oci_error = _ociexecute($stmt_list); $_row = array(); while (ocifetchinto($stmt_list, $_row, OCI_ASSOC + OCI_RETURN_LOBS + OCI_RETURN_NULLS)) { $_row = unserialize($_row['PINFEN_RULE']); if ($_row['pinfen_name'] && $_row['koufen_name'] && $_row['base_num'] && $_row['just_rule'] && $_row['pinfen_step'] && $_row['rule_num']) { $_row_infos[] = $_row; } } //获取V2级别的评分要求 $sql = "select * from {$this->report_monitor_config} t where pinfen_rule is not null "; $stmt_list = _ociparse($conn_db, $sql); $oci_error = _ociexecute($stmt_list); $_row = array(); while (ocifetchinto($stmt_list, $_row, OCI_ASSOC + OCI_RETURN_LOBS + OCI_RETURN_NULLS)) { $_row = unserialize($_row['PINFEN_RULE']); if ($_row['pinfen_name'] && $_row['koufen_name'] && $_row['base_num'] && $_row['just_rule'] && $_row['pinfen_step'] && $_row['rule_num']) { $_row_infos[] = $_row; } } print_r($_row_infos); foreach ($_row_infos as $_row_info) { if ($_row_info['v2']) { if ($_row_info['just_rule'] == '>') { $sql = "select case when nvl(t.fun_count,0) > :base_num then - round((nvl(t.fun_count,0) - :base_num) / :pinfen_step) else 0 end as num from {$this->report_monitor_date} t where v1 = :v1 and v2 = :v2 and cal_date = trunc(sysdate) "; } else { $sql = "select t.fun_count, case when nvl(t.fun_count,0) < :base_num then - round((:base_num - nvl(t.fun_count,0)) / :pinfen_step) else 0 end as num from {$this->report_monitor_date} t where v1 = :v1 and v2 = :v2 and cal_date = trunc(sysdate) "; } } else { if ($_row_info['just_rule'] == '>') { $sql = "select case when sum(nvl(t.fun_count,0)) > :base_num then - round(( sum(nvl(t.fun_count,0)) - :base_num) / :pinfen_step) else 0 end as num from {$this->report_monitor_date} t where v1 = :v1 and cal_date = trunc(sysdate) "; } else { $sql = "select case when sum(nvl(t.fun_count,0)) < :base_num then - round((:base_num - sum(nvl(t.fun_count,0)) ) / :pinfen_step) else 0 end as num from {$this->report_monitor_date} t where v1 = :v1 and cal_date = trunc(sysdate) "; } } $stmt = _ociparse($conn_db, $sql); _ocibindbyname($stmt, ':base_num', $_row_info['base_num']); _ocibindbyname($stmt, ':pinfen_step', $_row_info['pinfen_step']); _ocibindbyname($stmt, ':v1', $_row_info['v1']); if ($_row_info['v2']) { _ocibindbyname($stmt, ':v2', $_row_info['v2']); } $oci_error = _ociexecute($stmt); print_r($oci_error); $_row_num = array(); ocifetchinto($stmt, $_row_num, OCI_ASSOC + OCI_RETURN_LOBS + OCI_RETURN_NULLS); _status($_row_num['NUM'], $_row_info['pinfen_name'], $_row_info['koufen_name'], $_row_info['v1'] . "@" . $_row_info['v2']); print_r($_row_num); } }
function _initialize() { #每小时执行一次 if (date('i') != 30) { exit; } //清空之前xss文件,重新检测 $dirs = glob('/dev/shm/xss_' . VHOST . '/*'); foreach ($dirs as $k => $v) { unlink($v); } if (class_exists('memcache_config')) { $memcache_config = new memcache_config(); print_r($memcache_config->config); foreach ($memcache_config->config as $k => $v) { //不要检测 couchbase if (isset($v['bucket'])) { continue; } foreach ($v as $vv) { $memcache_server = new memcache_server(array($vv)); $memcache_server->connect('testkey'); $x = $memcache_server->memcacheObj->getStats(); $memcache_server->close(); _status($x["bytes"] / 1048576, VHOST . "(Memcache状态)", '已使用(M)', "{$memcache_server->current_host['host']}:{$memcache_server->current_host['port']}", NULL, VIP, 0, 'replace'); _status($x["limit_maxbytes"] / 1048576, VHOST . "(Memcache状态)", '总空间(M)', "{$memcache_server->current_host['host']}:{$memcache_server->current_host['port']}", NULL, VIP, 0, 'replace'); _status($x["curr_items"], VHOST . "(Memcache状态)", 'KEY个数', "{$memcache_server->current_host['host']}:{$memcache_server->current_host['port']}", NULL, VIP, 0, 'replace'); _status(round($x["uptime"] / 86400, 0), VHOST . "(Memcache状态)", '运行天数', "{$memcache_server->current_host['host']}:{$memcache_server->current_host['port']}", NULL, VIP, 0, 'replace'); } } } die('OK'); }
/** * @desc WHAT? * @author * @since 2013-07-14 15:55:31 * @throws 注意:无DB异常处理 */ function delete($key) { $cache_file = $this->_dir($key); if (is_file($cache_file)) { unlink($cache_file); _status(1, VHOST . "(本机缓存)", "删除缓存[命中]"); } else { _status(1, VHOST . "(本机缓存)", "删除缓存[未命中]"); } }
function _initialize() { #暂时关闭 exit; $time = date("Y-m-d H:i", strtotime("-1 minutes")); $num = exec("ls -R --full-time | grep '{$time}' | awk '{print \$7}' | awk -F ':' '{print \$1\":\"\$2}' |sort |uniq -c |sort -nr "); if ($num) { list($num_1, $time_1) = explode(' ', trim($num)); _status($num_1, VHOST . '(WEB日志分析)', '文件', NULL, $time_1); } }
/** * @desc WHAT? * @author * @since 2012-07-23 17:08:27 * @throws 注意:无DB异常处理 * _ftp('iosfile','/ftp/dir/','/home/httpd/ios/xxx.rar') -> ftp:/ftp/dir/xxx.rar */ function _ftp($configName, $dir, $file) { $ftp_config = new ftp_config(); $interfaceConfig = $ftp_config->config[$configName]; if (!$interfaceConfig) { return false; } //连接FTP $t1 = microtime(true); $bool = $connRes = ftp_connect($interfaceConfig['host']); $diff_time = sprintf('%.5f', microtime(true) - $t1); if ($diff_time > 3) { _status(1, VHOST . '(BUG错误)', 'FTP超时(连接)', "{$configName}" . "@" . GET_INCLUDED_FILES, NULL, VIP, $diff_time); } _status(1, VHOST . '(FTP)', $interfaceConfig['host'], GET_INCLUDED_FILES, NULL, VIP, $diff_time); $t1 = microtime(true); $bool = ftp_login($connRes, $interfaceConfig['user_name'], $interfaceConfig['user_pass']); if (!$bool) { return false; } if ($interfaceConfig['dir'] != '/') { $bool = ftp_chdir($connRes, $interfaceConfig['dir']); } $dir_array = explode('/', $dir); if (count($dir_array)) { foreach ($dir_array as $v) { if (!$v) { continue; } ftp_mkdir($connRes, $v); ftp_chdir($connRes, $v); } } $diff_time = sprintf('%.5f', microtime(true) - $t1); if ($diff_time > 3) { _status(1, VHOST . '(BUG错误)', 'FTP超时(登录切换目录)', "{$configName}" . "@" . GET_INCLUDED_FILES, NULL, VIP, $diff_time); } $t1 = microtime(true); //上传文件 ftp_pasv($connRes, true); $bool = ftp_put($connRes, basename($file), $file, FTP_BINARY); $diff_time = sprintf('%.5f', microtime(true) - $t1); if ($diff_time > 3) { _status(1, VHOST . '(BUG错误)', 'FTP超时(上传)', "{$configName}" . "@" . GET_INCLUDED_FILES, NULL, VIP, $diff_time); } $diff_time_str = _debugtime($diff_time); if ($diff_time < 1) { _status(1, VHOST . '(FTP)', '一秒内', _debugtime($diff_time), $interfaceConfig['host'] . "@" . GET_INCLUDED_FILES . VIP, $configName, $diff_time); } else { _status(1, VHOST . '(FTP)', '超时', _debugtime($diff_time), $interfaceConfig['host'] . "@" . GET_INCLUDED_FILES . VIP, $configName, $diff_time); } return ftp_close($connRes); }
function _initialize() { $IPCS = array(); $_IPCS = explode('|', $this->ipcs); foreach ($_IPCS as $k => $v) { if ($k % $_GET['total'] == $_GET['mod']) { $IPCS[] = $v; } } print_r($IPCS); foreach ($IPCS as $ipcs) { $ic = $cs = 0; $seg = msg_get_queue($ipcs, 0600); $msgtype = 1; $msg_array = array(); $monitor = array(); //读取队列数据 while (msg_receive($seg, $msgtype, $msgtype, 1024 * 1024 * 5, $msg_array, true, MSG_IPC_NOWAIT)) { $monitor[date('Y-m-d H', strtotime($msg_array['time']))][$msg_array['v1']][$msg_array['v2']][$msg_array['v3']][$msg_array['v4']][$msg_array['v5']]['uptype'] = $msg_array['uptype']; if ($msg_array['uptype'] == 'replace') { $monitor[date('Y-m-d H', strtotime($msg_array['time']))][$msg_array['v1']][$msg_array['v2']][$msg_array['v3']][$msg_array['v4']][$msg_array['v5']]['count'] = $msg_array['num']; } else { $monitor[date('Y-m-d H', strtotime($msg_array['time']))][$msg_array['v1']][$msg_array['v2']][$msg_array['v3']][$msg_array['v4']][$msg_array['v5']]['count'] += $msg_array['num']; } //最大耗时 $monitor[date('Y-m-d H', strtotime($msg_array['time']))][$msg_array['v1']][$msg_array['v2']][$msg_array['v3']][$msg_array['v4']][$msg_array['v5']]['diff_time'] = max($monitor[date('Y-m-d H', strtotime($msg_array['time']))][$msg_array['v1']][$msg_array['v2']][$msg_array['v3']][$msg_array['v4']][$msg_array['v5']]['diff_time'], $msg_array['diff_time']); if ($ic++ > 15 * 10000) { break; } } //压缩回去 foreach ($monitor as $time => $vtype) { foreach ($vtype as $type => $vhost) { foreach ($vhost as $host => $vact) { foreach ($vact as $act => $vkey) { foreach ($vkey as $key => $vhostip) { foreach ($vhostip as $hostip => $v) { $cs++; _status($v['count'], $type, $host, $act, $key, $hostip, abs($v['diff_time']), $v['uptype'], strtotime($time . ":00:00")); } } } } } } _status(($ic - $cs) / $ic * 100, VHOST . '(WEB日志分析)', '队列', '压缩比例', $ipcs, VIP, 0, 'replace'); unset($monitor); } }
function sendmsg($user) { $date = date('Ymd', TIMESTAMP); $mdate = date('Y-m-d', TIMESTAMP); $uid = $user['uid']; $log = array(); $query = DB::query("SELECT * FROM sign_log l LEFT JOIN my_tieba t ON t.tid=l.tid WHERE l.uid='{$uid}' AND l.date='{$date}' ORDER BY l.status DESC, l.tid ASC"); $i = 1; $message = <<<EOF <html><body> <style type="text/css"> div.wrapper * { font: 12px "Microsoft YaHei", arial, helvetica, sans-serif; word-break: break-all; } div.wrapper a { color: #15c; text-decoration: none; } div.wrapper a:active { color: #d14836; } div.wrapper a:hover { text-decoration: underline; } div.wrapper p { line-height: 20px; margin: 0 0 .5em; text-align: center; } div.wrapper .sign_title { font-size: 20px; line-height: 24px; } div.wrapper .result_table { width: 85%; margin: 0 auto; border-spacing: 0; border-collapse: collapse; } div.wrapper .result_table td { padding: 10px 5px; text-align: center; border: 1px solid #dedede; } div.wrapper .result_table tr { background: #d5d5d5; } div.wrapper .result_table tbody tr { background: #efefef; } div.wrapper .result_table tbody tr:nth-child(odd) { background: #fafafa; } </style> <div class="wrapper"> <p class="sign_title">贴吧签到助手 - 签到报告</p> <p>{$mdate}<br>若有大量贴吧签到失败,建议您重新设置 Cookie 相关信息</p> <table class="result_table"> <thead><tr><td style="width: 40px">#</td><td>贴吧</td><td style="width: 75px">状态</td><td style="width: 75px">经验</td></tr></thead> <tbody> EOF; while ($result = DB::fetch($query)) { $message .= '<tr><td>' . $i++ . "</td><td><a href=\"http://tieba.baidu.com/f?kw={$result[unicode_name]}\" target=\"_blank\">{$result[name]}</a></td><td>" . _status($result['status']) . '</td><td>' . _exp($result['exp']) . '</td></tr>'; $log[] = $result; } $message .= '</tbody></table></div></body></html>'; $res = send_mail($user['email'], "[{$mdate}] 贴吧签到助手 - {$user[username]} - 签到报告", $message); }
function _assign($name, $value) { global $_assigns; $stringValue = is_array($value) ? 'Array' : $value; _status("assigning '{$stringValue}' to '{$name}'"); return $_assigns[$name] = $value; }
function _initialize() { set_time_limit(0); ini_set("display_errors", true); echo "<pre>"; $conn_db = _ocilogon($this->db); if (!$conn_db) { return; } //每小时汇总[上小时+当前小时] $hourtime = strtotime(date('Y-m-d H:0:0') . " -1 hour"); $endtime = time(); if ($_GET['hour']) { $hourtime = strtotime($_GET['hour']); $endtime = strtotime("{$_GET['hour']} +1 day"); } //所有配置信息 包含虚列 $sql = "select * from {$this->report_monitor_config} t where id>0"; $stmt = _ociparse($conn_db, $sql); $oci_error = _ociexecute($stmt); $this->all_config = $_row = array(); while (ocifetchinto($stmt, $_row, OCI_ASSOC + OCI_RETURN_LOBS + OCI_RETURN_NULLS)) { $this->all_config[$_row['V1'] . $_row['V2']] = $_row; } $addwhere = null; if ($_GET['v1']) { $addwhere .= " and v1=:v1 "; } if ($_GET['v2']) { $addwhere .= " and v2=:v2 "; } for ($it = $hourtime; $it <= $endtime; $it += 3600) { $hour = date('Y-m-d H:00:00', $it); echo "hour:{$hour}\n"; //每小时数据汇总memory_max,memory_total, cpu_user_time_max,cpu_user_time_total,cpu_sys_time_max,cpu_sys_time_total $sql = "select to_char(t.cal_date, 'yyyy-mm-dd hh24') cal_date, t.v1, decode(t.v2,null,'null',v2) v2,\n decode(t.v3,null,'null',v3) v3, sum(fun_count) fun_count,avg(fun_count) fun_count_avg,max(abs(nvl(v6,0))) DIFF_TIME, sum(abs(t.total_diff_time)) total_diff_time,\n max(memory_max) memory_max, sum(memory_total) memory_total, max(cpu_user_time_max) cpu_user_time_max,sum(cpu_user_time_total) cpu_user_time_total, max(cpu_sys_time_max) cpu_sys_time_max, sum(cpu_sys_time_total) cpu_sys_time_total\n from {$this->report_monitor} t\n where cal_date >= to_date(:hour,'yyyy-mm-dd hh24:mi:ss') and cal_date <to_date(:hour,'yyyy-mm-dd hh24:mi:ss')+1/24\n {$addwhere}\n group by t.v1, t.v2,t.v3, to_char(t.cal_date, 'yyyy-mm-dd hh24') "; $stmt_list = _ociparse($conn_db, $sql); _ocibindbyname($stmt_list, ':hour', $hour); if ($_GET['v1']) { _ocibindbyname($stmt_list, ':v1', $_GET['v1']); } if ($_GET['v2']) { _ocibindbyname($stmt_list, ':v2', $_GET['v2']); } $oci_error = _ociexecute($stmt_list); print_r($oci_error); $_row = array(); while (ocifetchinto($stmt_list, $_row, OCI_ASSOC + OCI_RETURN_LOBS + OCI_RETURN_NULLS)) { $_row2 = $this->all_config[$_row['V1'] . $_row['V2']]; //正常情况下从原始表读取数据.如果是按照最后一分钟计算.走min表 //虚列数据不进行计算 if ($_row2['VIRTUAL_COLUMNS'] == 0) { if ($_row2['HOUR_COUNT_TYPE'] == 4) { $_row['FUN_COUNT'] = $_row['FUN_COUNT_AVG']; } $sql = "update {$this->report_monitor_hour} set fun_count=:fun_count,diff_time=:diff_time, total_diff_time=:total_diff_time,\n memory_max=:memory_max, memory_total=:memory_total, cpu_user_time_max=:cpu_user_time_max, cpu_user_time_total=:cpu_user_time_total, cpu_sys_time_max=:cpu_sys_time_max, cpu_sys_time_total=:cpu_sys_time_total\n where v1=:v1 and v2=:v2 and v3=:v3 and cal_date=to_date(:cal_date,'yyyy-mm-dd hh24') "; $stmt = _ociparse($conn_db, $sql); _ocibindbyname($stmt, ':v1', $_row['V1']); _ocibindbyname($stmt, ':v2', $_row['V2']); _ocibindbyname($stmt, ':v3', $_row['V3']); _ocibindbyname($stmt, ':cal_date', $_row['CAL_DATE']); _ocibindbyname($stmt, ':fun_count', $_row['FUN_COUNT']); _ocibindbyname($stmt, ':diff_time', abs($_row['DIFF_TIME'])); _ocibindbyname($stmt, ':total_diff_time', abs($_row['TOTAL_DIFF_TIME'])); _ocibindbyname($stmt, ':memory_max', $_row['MEMORY_MAX']); _ocibindbyname($stmt, ':memory_total', $_row['MEMORY_TOTAL']); _ocibindbyname($stmt, ':cpu_user_time_max', $_row['CPU_USER_TIME_MAX']); _ocibindbyname($stmt, ':cpu_user_time_total', $_row['CPU_USER_TIME_TOTAL']); _ocibindbyname($stmt, ':cpu_sys_time_max', $_row['CPU_SYS_TIME_MAX']); _ocibindbyname($stmt, ':cpu_sys_time_total', $_row['CPU_SYS_TIME_TOTAL']); $oci_error = _ociexecute($stmt); print_r($oci_error); _status(1, VHOST . "(PHPAPM)", "统计消耗", $_row['V1'], 'monitor_hour(update)', VIP); $ocirowcount = ocirowcount($stmt); if ($ocirowcount < 1) { $sql = "insert into {$this->report_monitor_hour} (cal_date,v1,v2,v3,fun_count,diff_time, total_diff_time,memory_max,memory_total, cpu_user_time_max,cpu_user_time_total,cpu_sys_time_max,cpu_sys_time_total)\n values (to_date(:cal_date,'yyyy-mm-dd hh24'),:v1,:v2,:v3,:fun_count,:diff_time, :total_diff_time, :memory_max,:memory_total, :cpu_user_time_max,:cpu_user_time_total,:cpu_sys_time_max,:cpu_sys_time_total) "; $stmt = _ociparse($conn_db, $sql); _ocibindbyname($stmt, ':v1', $_row['V1']); _ocibindbyname($stmt, ':v2', $_row['V2']); _ocibindbyname($stmt, ':v3', $_row['V3']); _ocibindbyname($stmt, ':cal_date', $_row['CAL_DATE']); _ocibindbyname($stmt, ':fun_count', $_row['FUN_COUNT']); _ocibindbyname($stmt, ':diff_time', abs($_row['DIFF_TIME'])); _ocibindbyname($stmt, ':total_diff_time', abs($_row['TOTAL_DIFF_TIME'])); _ocibindbyname($stmt, ':memory_max', $_row['MEMORY_MAX']); _ocibindbyname($stmt, ':memory_total', $_row['MEMORY_TOTAL']); _ocibindbyname($stmt, ':cpu_user_time_max', $_row['CPU_USER_TIME_MAX']); _ocibindbyname($stmt, ':cpu_user_time_total', $_row['CPU_USER_TIME_TOTAL']); _ocibindbyname($stmt, ':cpu_sys_time_max', $_row['CPU_SYS_TIME_MAX']); _ocibindbyname($stmt, ':cpu_sys_time_total', $_row['CPU_SYS_TIME_TOTAL']); $oci_error = _ociexecute($stmt); print_r($oci_error); if ($oci_error) { $get_included_files = basename(array_shift(get_included_files())); _status(1, VHOST . "(BUG错误)", 'SQL错误', $get_included_files . '/' . $_GET['act'], var_export($oci_error, true) . "|" . var_export($_row, true)); } else { _status(1, VHOST . "(PHPAPM)", "统计消耗", $_row['V1'], 'hour', VIP); } } //虚数列数据 $compare_group = array_filter(explode('|', '|' . $_row2['COMPARE_GROUP'])); if (count($compare_group) > 0) { foreach ($compare_group as $v) { $sql = "update {$this->report_monitor_hour} set fun_count=:fun_count,diff_time=:diff_time,total_diff_time=:total_diff_time,\n memory_max=:memory_max, memory_total=:memory_total, cpu_user_time_max=:cpu_user_time_max, cpu_user_time_total=:cpu_user_time_total, cpu_sys_time_max=:cpu_sys_time_max, cpu_sys_time_total=:cpu_sys_time_total\n where v1=:v1 and v2=:v2 and v3=:v3 and cal_date=to_date(:cal_date,'yyyy-mm-dd hh24') "; $stmt = _ociparse($conn_db, $sql); _ocibindbyname($stmt, ':v1', $v); _ocibindbyname($stmt, ':v2', $_row['V1'] . '_' . $_row['V2']); _ocibindbyname($stmt, ':v3', $_row['V3']); _ocibindbyname($stmt, ':cal_date', $_row['CAL_DATE']); _ocibindbyname($stmt, ':fun_count', $_row['FUN_COUNT']); _ocibindbyname($stmt, ':diff_time', abs($_row['DIFF_TIME'])); _ocibindbyname($stmt, ':total_diff_time', abs($_row['TOTAL_DIFF_TIME'])); _ocibindbyname($stmt, ':memory_max', $_row['MEMORY_MAX']); _ocibindbyname($stmt, ':memory_total', $_row['MEMORY_TOTAL']); _ocibindbyname($stmt, ':cpu_user_time_max', $_row['CPU_USER_TIME_MAX']); _ocibindbyname($stmt, ':cpu_user_time_total', $_row['CPU_USER_TIME_TOTAL']); _ocibindbyname($stmt, ':cpu_sys_time_max', $_row['CPU_SYS_TIME_MAX']); _ocibindbyname($stmt, ':cpu_sys_time_total', $_row['CPU_SYS_TIME_TOTAL']); $oci_error = _ociexecute($stmt); print_r($oci_error); _status(1, VHOST . "(PHPAPM)", "统计消耗", $_row['V1'], 'monitor_hour(update)', VIP); $ocirowcount = ocirowcount($stmt); if ($ocirowcount < 1) { $sql = "insert into {$this->report_monitor_hour} (cal_date,v1,v2,v3,fun_count,diff_time,total_diff_time,memory_max,memory_total, cpu_user_time_max,cpu_user_time_total,cpu_sys_time_max,cpu_sys_time_total)\n values (to_date(:cal_date,'yyyy-mm-dd hh24'),:v1,:v2,:v3,:fun_count,:diff_time,:total_diff_time, :memory_max,:memory_total, :cpu_user_time_max,:cpu_user_time_total,:cpu_sys_time_max,:cpu_sys_time_total) "; $stmt = _ociparse($conn_db, $sql); _ocibindbyname($stmt, ':v1', $v); _ocibindbyname($stmt, ':v2', $_row['V1'] . '_' . $_row['V2']); _ocibindbyname($stmt, ':v3', $_row['V3']); _ocibindbyname($stmt, ':cal_date', $_row['CAL_DATE']); _ocibindbyname($stmt, ':fun_count', $_row['FUN_COUNT']); _ocibindbyname($stmt, ':diff_time', abs($_row['DIFF_TIME'])); _ocibindbyname($stmt, ':total_diff_time', abs($_row['TOTAL_DIFF_TIME'])); _ocibindbyname($stmt, ':memory_max', $_row['MEMORY_MAX']); _ocibindbyname($stmt, ':memory_total', $_row['MEMORY_TOTAL']); _ocibindbyname($stmt, ':cpu_user_time_max', $_row['CPU_USER_TIME_MAX']); _ocibindbyname($stmt, ':cpu_user_time_total', $_row['CPU_USER_TIME_TOTAL']); _ocibindbyname($stmt, ':cpu_sys_time_max', $_row['CPU_SYS_TIME_MAX']); _ocibindbyname($stmt, ':cpu_sys_time_total', $_row['CPU_SYS_TIME_TOTAL']); $oci_error = _ociexecute($stmt); print_r($oci_error); if ($oci_error) { $get_included_files = basename(array_shift(get_included_files())); _status(1, VHOST . "(BUG错误)", 'SQL错误', $get_included_files . '/' . $_GET['act'], var_export($oci_error, true) . "|" . var_export($_row, true)); } else { _status(1, VHOST . "(PHPAPM)", "统计消耗", $_row['V1'], 'hour', VIP); } } } } } } } //刷新一天的数据 $sql = "select to_char(t.cal_date, 'yyyy-mm-dd') cal_date, t.v1, decode(t.v2,null,'null',v2) v2,\n sum(fun_count) fun_count,avg(fun_count) fun_count_avg from {$this->report_monitor_hour} t\n where cal_date >= to_date(:m_date,'yyyy-mm-dd') and cal_date<to_date(:m_date,'yyyy-mm-dd')+1 {$addwhere}\n group by t.v1, t.v2, to_char(t.cal_date, 'yyyy-mm-dd')"; $stmt_list = _ociparse($conn_db, $sql); echo htmlspecialchars($sql); var_dump(date("Y-m-d", $hourtime)); //print_r($_GET); _ocibindbyname($stmt_list, ':m_date', date("Y-m-d", $hourtime)); if ($_GET['v1']) { _ocibindbyname($stmt_list, ':v1', $_GET['v1']); } if ($_GET['v2']) { _ocibindbyname($stmt_list, ':v2', $_GET['v2']); } $oci_error = _ociexecute($stmt_list); print_r($oci_error); $_row = array(); while (ocifetchinto($stmt_list, $_row, OCI_ASSOC + OCI_RETURN_LOBS + OCI_RETURN_NULLS)) { //补全v1的信息 $sql = "select * from {$this->report_monitor_v1} where v1=:v1 "; $stmt = _ociparse($conn_db, $sql); _ocibindbyname($stmt, ':v1', $_row['V1']); $oci_error = _ociexecute($stmt); print_r($oci_error); $_row_config = array(); ocifetchinto($stmt, $_row_config, OCI_ASSOC + OCI_RETURN_LOBS + OCI_RETURN_NULLS); if (!$_row_config) { $sql = "insert into {$this->report_monitor_v1} (v1,id) values (:v1,seq_{$this->report_monitor}.nextval) "; $stmt = _ociparse($conn_db, $sql); _ocibindbyname($stmt, ':v1', $_row['V1']); $oci_error = _ociexecute($stmt); print_r($oci_error); _status(1, VHOST . "(PHPAPM)", "统计消耗", $_row['V1'], 'v1_config', VIP); } $_row_config = $this->all_config[$_row['V1'] . $_row['V2']]; //如果是不累计的,重置总量为上个小时的总量 if ($_row_config['DAY_COUNT_TYPE'] == 1 || $_row_config['DAY_COUNT_TYPE'] == 2 || $_row_config['DAY_COUNT_TYPE'] == 5 || $_row_config['DAY_COUNT_TYPE'] == 7) { //echo "只计算最后一小时\n"; $sql2 = "select to_char(max(cal_date),'yyyy-mm-dd hh24:mi:ss') cal_date from\n {$this->report_monitor_hour} where cal_date>=to_date(:cal_date,'yyyy-mm-dd')\n and cal_date<to_date(:cal_date,'yyyy-mm-dd')+1 and v1=:v1 and v2=:v2 "; $stmt2 = _ociparse($conn_db, $sql2); _ocibindbyname($stmt2, ':v1', $_row['V1']); _ocibindbyname($stmt2, ':v2', $_row['V2']); _ocibindbyname($stmt2, ':cal_date', $_row['CAL_DATE']); $oci_error2 = _ociexecute($stmt2); print_r($oci_error2); $_row2 = array(); ocifetchinto($stmt2, $_row2, OCI_ASSOC + OCI_RETURN_LOBS + OCI_RETURN_NULLS); //print_r($_row2); $sql = "select t.v1, t.v2, sum(fun_count) fun_count,avg(fun_count) fun_count_avg\n \t\t\tfrom {$this->report_monitor_hour} t where cal_date=to_date(:cal_date,'yyyy-mm-dd hh24:mi:ss')\n and v1=:v1 and v2=:v2 group by t.v1, t.v2"; $stmt = _ociparse($conn_db, $sql); _ocibindbyname($stmt, ':v1', $_row['V1']); _ocibindbyname($stmt, ':v2', $_row['V2']); _ocibindbyname($stmt, ':cal_date', $_row2['CAL_DATE']); $oci_error = _ociexecute($stmt); print_r($oci_error); $_row2 = array(); ocifetchinto($stmt, $_row2, OCI_ASSOC + OCI_RETURN_LOBS + OCI_RETURN_NULLS); $_row['FUN_COUNT'] = $_row2['FUN_COUNT']; //v3个数 if ($_row_config['DAY_COUNT_TYPE'] == 7) { //echo "计算V3个数\n"; $sql = "select count(distinct(t.v3)) num\n \t\t\tfrom {$this->report_monitor_hour} t where cal_date>=to_date(:cal_date,'yyyy-mm-dd')\n and v1=:v1 and v2=:v2"; $stmt = _ociparse($conn_db, $sql); _ocibindbyname($stmt, ':v1', $_row['V1']); _ocibindbyname($stmt, ':v2', $_row['V2']); _ocibindbyname($stmt, ':cal_date', $_row['CAL_DATE']); $oci_error = _ociexecute($stmt); print_r($oci_error); ocifetchinto($stmt, $_row2, OCI_ASSOC + OCI_RETURN_LOBS + OCI_RETURN_NULLS); $_row['FUN_COUNT'] = $_row2['NUM']; //echo " num:{$_row['FUN_COUNT']} \n"; } //最后一小时的平均值 if ($_row_config['DAY_COUNT_TYPE'] == 5) { $_row['FUN_COUNT'] = $_row2['FUN_COUNT_AVG']; } } //当天的平均数 if ($_row_config['DAY_COUNT_TYPE'] == 6) { $_row['FUN_COUNT'] = $_row['FUN_COUNT_AVG']; } //print_r($_row); //echo " num:{$_row['FUN_COUNT']} \n"; $sql = "update {$this->report_monitor_date} set fun_count=:fun_count\n where v1=:v1 and v2=:v2 and cal_date=to_date(:cal_date,'yyyy-mm-dd') "; $stmt2 = _ociparse($conn_db, $sql); _ocibindbyname($stmt2, ':v1', $_row['V1']); _ocibindbyname($stmt2, ':v2', $_row['V2']); _ocibindbyname($stmt2, ':cal_date', $_row['CAL_DATE']); _ocibindbyname($stmt2, ':fun_count', $_row['FUN_COUNT']); $oci_error = _ociexecute($stmt2); print_r($oci_error); _status(1, VHOST . "(PHPAPM)", "统计消耗", $_row['V1'], 'monitor_date(update)', VIP); $_row_count = ocirowcount($stmt2); if (!$_row_count) { $sql = "insert into {$this->report_monitor_date} (cal_date,v1,v2,fun_count) values\n (to_date(:cal_date,'yyyy-mm-dd'),:v1,:v2,:fun_count) "; $stmt = _ociparse($conn_db, $sql); _ocibindbyname($stmt, ':v1', $_row['V1']); _ocibindbyname($stmt, ':v2', $_row['V2']); _ocibindbyname($stmt, ':cal_date', $_row['CAL_DATE']); _ocibindbyname($stmt, ':fun_count', $_row['FUN_COUNT']); $oci_error = _ociexecute($stmt); print_r($oci_error); _status(1, VHOST . "(PHPAPM)", "统计消耗", $_row['V1'], 'date', VIP); } $compare_group = array_filter(explode('|', '|' . $_row_config['COMPARE_GROUP'])); if (count($compare_group) > 0) { foreach ($compare_group as $v) { $sql = "update {$this->report_monitor_date} set fun_count=:fun_count\n where v1=:v1 and v2=:v2 and cal_date=to_date(:cal_date,'yyyy-mm-dd') "; $stmt2 = _ociparse($conn_db, $sql); _ocibindbyname($stmt2, ':v1', $v); _ocibindbyname($stmt2, ':v2', $_row['V1'] . '_' . $_row['V2']); _ocibindbyname($stmt2, ':cal_date', $_row['CAL_DATE']); _ocibindbyname($stmt2, ':fun_count', $_row['FUN_COUNT']); $oci_error = _ociexecute($stmt2); print_r($oci_error); _status(1, VHOST . "(PHPAPM)", "统计消耗", $_row['V1'], 'monitor_date(update)', VIP); $_row_count = ocirowcount($stmt2); if (!$_row_count) { $sql = "insert into {$this->report_monitor_date} (cal_date,v1,v2,fun_count) values\n (to_date(:cal_date,'yyyy-mm-dd'),:v1,:v2,:fun_count) "; $stmt = _ociparse($conn_db, $sql); _ocibindbyname($stmt, ':v1', $v); _ocibindbyname($stmt, ':v2', $_row['V1'] . '_' . $_row['V2']); _ocibindbyname($stmt, ':cal_date', $_row['CAL_DATE']); _ocibindbyname($stmt, ':fun_count', $_row['FUN_COUNT']); $oci_error = _ociexecute($stmt); print_r($oci_error); _status(1, VHOST . "(PHPAPM)", "统计消耗", $_row['V1'], 'date', VIP); } } } if (!$_row_config) { $sql = "select count(*) c from {$this->report_monitor_config} where v1=:v1 "; $stmt = _ociparse($conn_db, $sql); _ocibindbyname($stmt, ':v1', $_row['V1']); $oci_error = _ociexecute($stmt); print_r($oci_error); $_row2 = array(); ocifetchinto($stmt, $_row2, OCI_ASSOC + OCI_RETURN_LOBS + OCI_RETURN_NULLS); $sql = "select * from {$this->report_monitor_v1} where v1=:v1 "; $stmt = _ociparse($conn_db, $sql); _ocibindbyname($stmt, ':v1', $_row['V1']); $oci_error = _ociexecute($stmt); print_r($oci_error); $_row3 = array(); ocifetchinto($stmt, $_row3, OCI_ASSOC + OCI_RETURN_LOBS + OCI_RETURN_NULLS); $sql = "insert into {$this->report_monitor_config} (v1,v2,orderby,id,day_count_type,hour_count_type,percent_count_type)\n values (:v1,:v2,:orderby,seq_{$this->report_monitor}.nextval,:day_count_type,:hour_count_type,:percent_count_type) "; $stmt = _ociparse($conn_db, $sql); _ocibindbyname($stmt, ':v1', $_row['V1']); _ocibindbyname($stmt, ':v2', $_row['V2']); _ocibindbyname($stmt, ':day_count_type', intval($_row3['DAY_COUNT_TYPE'])); _ocibindbyname($stmt, ':hour_count_type', intval($_row3['HOUR_COUNT_TYPE'])); _ocibindbyname($stmt, ':percent_count_type', intval($_row3['PERCENT_COUNT_TYPE'])); if ($_row['V2'] == '汇总') { _ocibindbyname($stmt, ':orderby', intval(0)); } else { _ocibindbyname($stmt, ':orderby', max(1, $_row2['C'] + 1)); } $oci_error = _ociexecute($stmt); print_r($oci_error); _status(1, VHOST . "(PHPAPM)", "统计消耗", $_row['V1'], 'config', VIP); } } //清除过期数据 if ($_GET['del'] && rand(1, 10) == 1) { $sql = "delete from {$this->report_monitor} where cal_date<=sysdate-10 "; $stmt_list = _ociparse($conn_db, $sql); $oci_error = _ociexecute($stmt_list); print_r($oci_error); } }
function _initialize() { #每小时执行一次 if (date('i') != 30) { exit; } $conn_db = _ocilogon($this->db); _ociexecute(_ociparse($conn_db, "alter session set nls_date_format='YYYY-MM-DD HH24:MI:SS'")); $sql = "select * from {$this->report_monitor_v1} where IS_DUTY=1 "; $stmt = _ociparse($conn_db, $sql); _ociexecute($stmt); $v1_all = $_row_all = array(); while (ocifetchinto($stmt, $_row_all, OCI_ASSOC + OCI_RETURN_LOBS + OCI_RETURN_NULLS)) { $v1_all[$_row_all['V1']] = $_row_all['V1']; } $sql = "select t.lookup, trunc(t.cal_date) cal_date, v1\n from {$this->report_monitor_date} t\n where t.cal_date >= trunc(sysdate - 7)\n and t.cal_date < trunc(sysdate - 6) and t.lookup is null\n group by trunc(t.cal_date), t.lookup, v1 "; $stmt = _ociparse($conn_db, $sql); _ociexecute($stmt); $_row = array(); while (ocifetchinto($stmt, $_row, OCI_ASSOC + OCI_RETURN_LOBS + OCI_RETURN_NULLS)) { if ($v1_all[$_row['V1']]) { continue; } _status(1, VHOST . "(BUG错误)", "验收责任未到位", $_row['V1'], "", VIP); } if ($_GET['no_manyi']) { return; } //技术基础分 _status(100, VHOST . "(项目满意分)", "基础分", "基础分", "基础分", VIP, 0, 'replace'); //错误率占10% $sql = "select (select nvl(sum(fun_count), 0)\n from {$this->report_monitor_date} t\n where v1 like '%(BUG错误)'\n and v2 = 'SQL错误'\n and t.cal_date = trunc(sysdate)) php_num,\n (select nvl(sum(fun_count), 0)\n from {$this->report_monitor_date} t\n where v1 like '%(BUG错误)'\n and v2 = 'PHP错误'\n and t.cal_date = trunc(sysdate)) sql_num,\n (select sum(t.fun_count)\n from {$this->report_monitor_date} t\n where v1 like '%(WEB日志分析)'\n and t.cal_date = trunc(sysdate)) web_num from dual "; $stmt = _ociparse($conn_db, $sql); $oci_error = _ociexecute($stmt); $_row = array(); ocifetchinto($stmt, $_row, OCI_ASSOC + OCI_RETURN_LOBS + OCI_RETURN_NULLS); $_row['SQLERR'] = round(($_row['PHP_NUM'] + $_row['SQL_NUM']) / $_row['WEB_NUM'] * 100, 2); $manyi = 0; if ($_row['SQLERR'] > 1) { $manyi = -10; } elseif ($_row['SQLERR'] < 0.1) { $manyi = 0; } else { $manyi = -($_row['SQLERR'] * 10); } _status($manyi, VHOST . "(项目满意分)", "PHP+SQL错误率", "PHP+SQL错误率", "PHP_NUM:{$_row['PHP_NUM']},SQL_NUM:{$_row['SQL_NUM']},WEB_NUM:{$_row['WEB_NUM']}@{$_row['SQLERR']}%", VIP, 0, 'replace'); //sql量40% $sql = "select (select nvl(sum(fun_count), 0)\n from {$this->report_monitor_date} t\n where v1 like '%(SQL统计)'\n and t.cal_date = trunc(sysdate - 2/24)) sql_num,\n (select sum(t.fun_count)\n from {$this->report_monitor_date} t\n where v1 like '%(WEB日志分析)'\n and t.cal_date = trunc(sysdate - 2/24)) web_num,\n (select sum(t.fun_count)\n from {$this->report_monitor_date} t\n where v1 like '%(WEB日志分析)'\n and t.cal_date = trunc(sysdate - 1)) y_web_num\n from dual "; $stmt = _ociparse($conn_db, $sql); $oci_error = _ociexecute($stmt); $_row = array(); ocifetchinto($stmt, $_row, OCI_ASSOC + OCI_RETURN_LOBS + OCI_RETURN_NULLS); $_row['SQLERR'] = round($_row['SQL_NUM'] / $_row['WEB_NUM'] * 100, 2); $manyi = 0; //按照前一天web量 判断 if ($_row['Y_WEB_NUM'] >= 2000000) { if ($_row['SQLERR'] > 1 * 100) { $manyi = -40; } elseif ($_row['SQLERR'] < 6 / 10 * 100) { $manyi = 0; } else { $manyi = -(100 - ($_row['SQLERR'] - 6 / 10 * 100) / (1 * 100 - 6 / 10 * 100) * 100) * 40 / 100; } } if ($_row['Y_WEB_NUM'] < 2000000 && $_row['Y_WEB_NUM'] >= 300000) { if ($_row['SQLERR'] > 2 * 100) { $manyi = -40; } elseif ($_row['SQLERR'] < 1.2 * 100) { $manyi = 0; } else { $manyi = -(100 - ($_row['SQLERR'] - 1.2 * 100) / (2 * 100 - 1.2 * 100) * 100) * 40 / 100; } } if ($_row['Y_WEB_NUM'] < 300000) { if ($_row['SQLERR'] > 50 * 100) { $manyi = -40; } elseif ($_row['SQLERR'] < 30 * 100) { $manyi = 0; } else { $manyi = -(100 - ($_row['SQLERR'] - 30 * 100) / (50 * 100 - 30 * 100) * 100) * 40 / 100; } } _status($manyi, VHOST . "(项目满意分)", "SQL回源率", "SQL回源率", "SQL_NUM:{$_row['SQL_NUM']},WEB_NUM:{$_row['WEB_NUM']}@{$_row['SQLERR']}%", VIP, 0, 'replace'); //单小时sql上限 $sql = "select nvl(sum(fun_count), 0) sql_num\n from {$this->report_monitor_date} t\n where v1 like '%(SQL统计)'\n and t.cal_date = trunc(sysdate)"; $stmt = _ociparse($conn_db, $sql); $oci_error = _ociexecute($stmt); $_row = array(); ocifetchinto($stmt, $_row, OCI_ASSOC + OCI_RETURN_LOBS + OCI_RETURN_NULLS); //扣分单小时SQL上限 $hour = date('H'); $manyi = 0; $sql_error = round($_row['SQL_NUM'] / $hour); if ($sql_error >= 300000) { $num = 5 * intval(($sql_error - 300000) / 10000); $manyi = $manyi - $num; } _status($manyi, VHOST . "(项目满意分)", "扣分:单小时SQL上限", "扣分:单小时SQL上限", "SQL_NUM:{$_row['SQL_NUM']},H:{$hour},平均sql量:{$sql_error}", VIP, 0, 'replace'); //memcache 20% $sql = "select (select nvl(sum(fun_count), 0)\n from {$this->report_monitor_date} t\n where v1 like '%(Memcache)'\n and t.cal_date = trunc(sysdate)) mem_num,\n (select sum(t.fun_count)\n from {$this->report_monitor_date} t\n where v1 like '%(WEB日志分析)'\n and t.cal_date = trunc(sysdate)) web_num from dual "; $stmt = _ociparse($conn_db, $sql); $oci_error = _ociexecute($stmt); $_row = array(); ocifetchinto($stmt, $_row, OCI_ASSOC + OCI_RETURN_LOBS + OCI_RETURN_NULLS); $_row['SQLERR'] = round($_row['MEM_NUM'] / $_row['WEB_NUM']); $manyi = 0; if ($_row['SQLERR'] > 6) { $manyi = -20; } elseif ($_row['SQLERR'] < 3) { $manyi = 0; } else { $manyi = -(100 - $_row['SQLERR'] / (6 - 3) * 100) * 20 / 100; } _status($manyi, VHOST . "(项目满意分)", "Memcache回源率", "Memcache回源率", "MEM_NUM:{$_row['MEM_NUM']},WEB_NUM:{$_row['WEB_NUM']}@" . $_row['SQLERR'] * 100 . "%", VIP, 0, 'replace'); $sql = "select sum(t.fun_count) sqlerr\n from {$this->report_monitor_hour} t\n where v1 like '%(BUG错误)'\n and v2 = '验收责任未到位'\n and t.cal_date = trunc(sysdate-1/24,'hh24') "; $stmt = _ociparse($conn_db, $sql); $oci_error = _ociexecute($stmt); $_row = array(); ocifetchinto($stmt, $_row, OCI_ASSOC + OCI_RETURN_LOBS + OCI_RETURN_NULLS); $manyi = 10; if ($_row['SQLERR'] > 0) { $manyi = -10; } _status($manyi, VHOST . "(项目满意分)", "项目验收", "项目验收", $_row['SQLERR'], VIP, 0, 'replace'); //tcp满意度 30% $sql = "select nvl(sum(fun_count), 0) TCP\n from {$this->report_monitor_date} t\n where v1 like '%(WEB日志分析)' and V2='TCP连接'\n and t.cal_date = trunc(sysdate-1/24)"; $stmt = _ociparse($conn_db, $sql); $oci_error = _ociexecute($stmt); $_row = array(); ocifetchinto($stmt, $_row, OCI_ASSOC + OCI_RETURN_LOBS + OCI_RETURN_NULLS); $manyi = 0; if ($_row['TCP'] > 120) { //tcp超120 每增加10扣100 无上限 $manyi = -(floor(($_row['TCP'] - 120) / 10) * 100); } elseif ($_row['TCP'] < 70) { $manyi = 0; } else { $manyi = 0 - (70 - ($_row['TCP'] - 70) / 100 * 100) * 30 / 100; } _status($manyi, VHOST . "(项目满意分)", "TCP连接数", "TCP连接数", 'TCP连接数:' . $_row['TCP'], VIP, 0, 'replace'); //扣分项 //机器重启当天,每小时扣200分 $sql = "select fun_count from {$this->report_monitor_date} t where v1 like'%(WEB日志分析)' and v2='运行天数' and t.cal_date = trunc(sysdate - 1/24 )"; $stmt = _ociparse($conn_db, $sql); $oci_error = _ociexecute($stmt); $_row = array(); $manyi = 0; ocifetchinto($stmt, $_row, OCI_ASSOC + OCI_RETURN_LOBS + OCI_RETURN_NULLS); if ($_row['FUN_COUNT'] == '') { $manyi = -200; } _status($manyi, VHOST . "(项目满意分)", "扣分:机器重启", "机器重启", NULL, VIP, 0, 'replace'); //非定时任务扣分(非定时任务代码执行超过1秒占总量的0.1%以上,扣20分) $sql = "select (select nvl(sum(fun_count), 0)\n from {$this->report_monitor_date} t\n where v1 like '%(BUG错误)' and (v2 ='超时')\n and t.cal_date = trunc(sysdate)) sql_num,\n (select sum(t.fun_count)\n from {$this->report_monitor_date} t\n where v1 like '%(BUG错误)'\n and t.cal_date = trunc(sysdate)) web_num\n from dual "; $stmt = _ociparse($conn_db, $sql); $oci_error = _ociexecute($stmt); $_row = array(); ocifetchinto($stmt, $_row, OCI_ASSOC + OCI_RETURN_LOBS + OCI_RETURN_NULLS); $_row['SQLERR'] = $_row['SQL_NUM'] / $_row['WEB_NUM']; $manyi = 0; if ($_row['SQL_NUM'] >= 1000) { $num = 5 * intval($_row['SQL_NUM'] / 1000); $manyi = $manyi - $num; } _status($manyi, VHOST . "(项目满意分)", "扣分:执行超时", "执行超时", "OVER_NUM:{$_row['SQL_NUM']}", VIP, 0, 'replace'); //问题sql扫描 $sql = "select fun_count from {$this->report_monitor_date} t where v1 like'%(问题SQL)' and v2='全表扫描' and t.cal_date = trunc(sysdate-1/24)"; $stmt = _ociparse($conn_db, $sql); $oci_error = _ociexecute($stmt); $_row = array(); $manyi = 0; ocifetchinto($stmt, $_row, OCI_ASSOC + OCI_RETURN_LOBS + OCI_RETURN_NULLS); if ($_row['FUN_COUNT'] > 5) { $manyi = -10; } else { $manyi = -($_row['FUN_COUNT'] * 2.5); } _status($manyi, VHOST . "(项目满意分)", "扣分:问题sql", "全表扫描", '问题SQL' . $_row['FUN_COUNT'], VIP, 0, 'replace'); // CPU>8 或者 LOAD>8 扣10分 $sql = "select nvl(avg(fun_count), 0) CPU\n from {$this->report_monitor_date} t\n where v1 like '%(WEB日志分析)' and V2='CPU'\n and t.cal_date = trunc(sysdate-1/24)"; $stmt = _ociparse($conn_db, $sql); $oci_error = _ociexecute($stmt); $_row = array(); ocifetchinto($stmt, $_row, OCI_ASSOC + OCI_RETURN_LOBS + OCI_RETURN_NULLS); $sql = "select nvl(avg(fun_count), 0) LOAD\n from {$this->report_monitor_date} t\n where v1 like '%(WEB日志分析)' and V2='Load'\n and t.cal_date = trunc(sysdate-1/24)"; $stmt = _ociparse($conn_db, $sql); $oci_error = _ociexecute($stmt); $_row_load = array(); ocifetchinto($stmt, $_row_load, OCI_ASSOC + OCI_RETURN_LOBS + OCI_RETURN_NULLS); $manyi = 0; if ($_row['CPU'] > 8 || $_row_load['LOAD'] > 8) { $manyi = -10; } _status($manyi, VHOST . "(项目满意分)", "扣分:CPU LOAD", "CPU或LOAD过高", "CPU:{$_row['CPU']};LOAD:{$_row_load['LOAD']}", VIP, 0, 'replace'); //web 500扣分 WEB日志出现5xx错误 [占0.05% 扣1分,没加一个万分点,扣1分,无上限] $sql = "select (select nvl(sum(fun_count), 0)\n from {$this->report_monitor_date} t\n where v1 like '%(WEB日志分析)' and v2 like '5%'\n and t.cal_date = trunc(sysdate)) err_num,\n (select sum(t.fun_count)\n from {$this->report_monitor_date} t\n where v1 like '%(WEB日志分析)'\n and t.cal_date = trunc(sysdate)) web_num from dual "; $stmt = _ociparse($conn_db, $sql); $oci_error = _ociexecute($stmt); $_row = array(); ocifetchinto($stmt, $_row, OCI_ASSOC + OCI_RETURN_LOBS + OCI_RETURN_NULLS); $manyi = 0; $sql = "select nvl(sum(fun_count), 0) err_t_num\n from {$this->report_monitor_date} t\n where v1 like '%(WEB日志分析)' and v2 = '499'\n and t.cal_date = trunc(sysdate)"; $stmt = _ociparse($conn_db, $sql); _ociexecute($stmt); $_row_t = array(); ocifetchinto($stmt, $_row_t, OCI_ASSOC + OCI_RETURN_LOBS + OCI_RETURN_NULLS); if ($_row_t['ERR_T_NUM'] > 500) { $_row['ERR_NUM'] = $_row['ERR_NUM'] + $_row_t['ERR_T_NUM']; } $_row['SQLERR'] = round($_row['ERR_NUM'] / $_row['WEB_NUM'], 4); if ($_row['SQLERR'] >= 0.0005) { $manyi = ($manyi - ($_row['SQLERR'] - 0.0005)) * 10000; } _status($manyi, VHOST . "(项目满意分)", "扣分:5xx错误", "5xx错误", "ERR_NUM:{$_row['ERR_NUM']},WEB_NUM:{$_row['WEB_NUM']}@" . $_row['SQLERR'] * 10000 . "万分", VIP, 0, 'replace'); //[扣分:包含文件] "10个到∞个"每个扣除5分 $sql = "select nvl(sum(fun_count), 0) fun_count\n from {$this->report_monitor_date} t\n where v1 like '%(包含文件)' and V2='10s到∞个'\n and t.cal_date = trunc(sysdate) "; $stmt = _ociparse($conn_db, $sql); $oci_error = _ociexecute($stmt); $_row = array(); ocifetchinto($stmt, $_row, OCI_ASSOC + OCI_RETURN_LOBS + OCI_RETURN_NULLS); $manyi = 0; if ($_row['FUN_COUNT']) { $manyi = $manyi - $_row['FUN_COUNT'] * 5; } _status($manyi, VHOST . "(项目满意分)", "扣分:包含文件", "包含文件", "包含文件个数:{$_row['FUN_COUNT']}", VIP, 0, 'replace'); $manyi = 0; //扣分:安全事故 $sql = "select nvl(sum(fun_count), 0) C**K\n from {$this->report_monitor_hour} t\n where v1 like '%(BUG错误)' and V2='上传木马入侵'\n and t.cal_date= trunc(sysdate-1/24,'hh24')"; $stmt = _ociparse($conn_db, $sql); _ociexecute($stmt); $_row_cock = array(); ocifetchinto($stmt, $_row_cock, OCI_ASSOC + OCI_RETURN_LOBS + OCI_RETURN_NULLS); $manyi = $manyi - $_row_cock['C**K'] * 50; _status($manyi, VHOST . "(项目满意分)", "扣:安全", "安全事故", "入侵个数:{$_row_cock['C**K']}", VIP, 0, 'replace'); //扣分:故障事故 $sql = "select fun_count,v3,to_char(cal_date,'yyyy-mm-dd hh24') cal_date\n from {$this->report_monitor_hour} t\n where v1 like '%(BUG错误)' and V2='PHP错误'\n and t.cal_date>= trunc(sysdate-1,'hh24') order by cal_date desc"; $stmt = _ociparse($conn_db, $sql); _ociexecute($stmt); $_row_php = array(); $manyi = 0; $data = $arr = array(); $time = date('Y-m-d H', time() - 3600); while (ocifetchinto($stmt, $_row_php, OCI_ASSOC + OCI_RETURN_LOBS + OCI_RETURN_NULLS)) { $data[$_row_php['V3']][$_row_php['CAL_DATE']]['count'] = $_row_php['FUN_COUNT']; } foreach ($data as $k => $v) { if (!isset($v[$time]['count']) || $v[$time]['count'] <= 0) { unset($data[$k]); } else { for ($i = time() - 3600; $i >= time() - 3600 * 24; $i--) { $i_time = date('Y-m-d H', $i); if (!isset($v[$time]) || $v[$i_time]['count'] <= 0) { break; } else { $arr[$k][$i_time] = $v[$i_time]['count']; } } } } foreach ($arr as $k => $v) { if (count($v) >= 6) { $manyi = $manyi - (count($v) - 5) * 100; } } _status($manyi, VHOST . "(项目满意分)", "扣:故障", "故障事故", NULL, VIP, 0, 'replace'); }
function _initialize() { ini_set("display_errors", true); $xxi = 0; $conn_db = _ocilogon($this->db); if (!$conn_db) { exit('no db'); } $get_included_files = basename(array_shift(get_included_files())); $tt1 = microtime(true); echo "<pre> 准备压缩数据:\n"; $monitor_count = $files = $monitor = $monitor_min = array(); $ic = 0; $config_data = array(); $sql = "select * from {$this->report_monitor_queue} order by id desc LIMIT 0, 2000"; $stmt = _ociparse($conn_db, $sql); _ociexecute($stmt); $_row = array(); while (ocifetchinto($stmt, $_row, OCI_ASSOC + OCI_RETURN_LOBS + OCI_RETURN_NULLS)) { $msg_array = unserialize($_row['QUEUE']); if ($msg_array['v5'] == null) { $msg_array['v5'] = VIP; } //专门对付SQL不规范的写法 if (strpos($msg_array['v1'], 'SQL') !== false) { $out = array(); preg_match('# in(\\s+)?\\(#is', $msg_array['v4'], $out); if ($out) { $msg_array['v4'] = substr($msg_array['v4'], 0, strpos($msg_array['v4'], ' in')) . ' in....'; } } if (strpos($msg_array['v1'], 'SQL') !== false) { preg_match('# in(\\s+)?\\(#is', $msg_array['v3'], $out); if ($out) { $msg_array['v3'] = substr($msg_array['v3'], 0, strpos($msg_array['v3'], ' in')) . ' in....'; } } foreach ((array) $msg_array['includes'] as $file) { $files[$msg_array['vhost']][$file] = $file; } //查看命中了哪些监控 $config_data[$msg_array['v1']][$msg_array['v2']]++; //日志数据,不会被删除 $monitor[date('Y-m-d H', strtotime($msg_array['time']))][$msg_array['v1']][$msg_array['v2']][$msg_array['v3']][$msg_array['v4']][$msg_array['v5']]['uptype'] = $msg_array['uptype']; if ($msg_array['uptype'] == 'replace') { $monitor[date('Y-m-d H', strtotime($msg_array['time']))][$msg_array['v1']][$msg_array['v2']][$msg_array['v3']][$msg_array['v4']][$msg_array['v5']]['count'] = $msg_array['num']; } else { $monitor[date('Y-m-d H', strtotime($msg_array['time']))][$msg_array['v1']][$msg_array['v2']][$msg_array['v3']][$msg_array['v4']][$msg_array['v5']]['count'] += $msg_array['num']; } //最大耗时 $monitor[date('Y-m-d H', strtotime($msg_array['time']))][$msg_array['v1']][$msg_array['v2']][$msg_array['v3']][$msg_array['v4']][$msg_array['v5']]['diff_time'] = max($monitor[date('Y-m-d H', strtotime($msg_array['time']))][$msg_array['v1']][$msg_array['v2']][$msg_array['v3']][$msg_array['v4']][$msg_array['v5']]['diff_time'], abs($msg_array['diff_time'])); //总耗时 $monitor[date('Y-m-d H', strtotime($msg_array['time']))][$msg_array['v1']][$msg_array['v2']][$msg_array['v3']][$msg_array['v4']][$msg_array['v5']]['total_diff_time'] += abs($msg_array['diff_time']); //内存单次最大消耗 $monitor[date('Y-m-d H', strtotime($msg_array['time']))][$msg_array['v1']][$msg_array['v2']][$msg_array['v3']][$msg_array['v4']][$msg_array['v5']]['memory_max'] = max($monitor[date('Y-m-d H', strtotime($msg_array['time']))][$msg_array['v1']][$msg_array['v2']][$msg_array['v3']][$msg_array['v4']][$msg_array['v5']]['memory_max'], abs($msg_array['memory'])); //内存消耗.总 $monitor[date('Y-m-d H', strtotime($msg_array['time']))][$msg_array['v1']][$msg_array['v2']][$msg_array['v3']][$msg_array['v4']][$msg_array['v5']]['memory_total'] += abs($msg_array['memory']); // 用户消耗CPU,单次最大 $monitor[date('Y-m-d H', strtotime($msg_array['time']))][$msg_array['v1']][$msg_array['v2']][$msg_array['v3']][$msg_array['v4']][$msg_array['v5']]['cpu_user_time_max'] = max($monitor[date('Y-m-d H', strtotime($msg_array['time']))][$msg_array['v1']][$msg_array['v2']][$msg_array['v3']][$msg_array['v4']][$msg_array['v5']]['cpu_user_time_max'], abs($msg_array['user_cpu'])); //用户消耗CPU,总 $monitor[date('Y-m-d H', strtotime($msg_array['time']))][$msg_array['v1']][$msg_array['v2']][$msg_array['v3']][$msg_array['v4']][$msg_array['v5']]['cpu_user_time_total'] += abs($msg_array['user_cpu']); //系统消耗CPU,单次最大 $monitor[date('Y-m-d H', strtotime($msg_array['time']))][$msg_array['v1']][$msg_array['v2']][$msg_array['v3']][$msg_array['v4']][$msg_array['v5']]['cpu_sys_time_max'] = max($monitor[date('Y-m-d H', strtotime($msg_array['time']))][$msg_array['v1']][$msg_array['v2']][$msg_array['v3']][$msg_array['v4']][$msg_array['v5']]['cpu_sys_time_max'], abs($msg_array['sys_cpu'])); //系统消耗CPU,总 $monitor[date('Y-m-d H', strtotime($msg_array['time']))][$msg_array['v1']][$msg_array['v2']][$msg_array['v3']][$msg_array['v4']][$msg_array['v5']]['cpu_sys_time_total'] += abs($msg_array['sys_cpu']); $monitor_count[md5(date('Y-m-d H', strtotime($msg_array['time'])) . $msg_array['v1'] . $msg_array['v2'] . $msg_array['v3'] . $msg_array['v4'] . $msg_array['v5'])] = 1; if ($ic++ > 10 * 10000) { break; } } //clear queue start $sql_d = "TRUNCATE {$this->report_monitor_queue}"; $stmt_d = _ociparse($conn_db, $sql_d); _ociexecute($stmt_d); //clear queue end $diff_time = sprintf('%.5f', microtime(true) - $tt1); echo "\n从{$ic}个压缩到" . count($monitor_count) . "(耗时:{$diff_time})\n"; echo "命中的类型:\n"; print_r($config_data); echo "\n\n"; $conn_db = _ocilogon($this->db); foreach ($monitor as $time => $vtype) { foreach ($vtype as $type => $vhost) { foreach ($vhost as $host => $vact) { foreach ($vact as $act => $vkey) { foreach ($vkey as $key => $vhostip) { foreach ($vhostip as $hostip => $v) { if (!$host) { $host = 'null'; } //截取4000字节 if (strlen($key) > 4000) { $key = substr($key, 0, 4000); } if (strlen($hostip) > 200) { $hostip = substr($hostip, 0, 200); } if (strlen($act) > 200) { $act = substr($act, 0, 200); } //去掉回车 $act = strtr($act, array("\n" => null, "\r" => null)); if ($v['uptype'] == 'replace') { //memory_max=,memory_total, cpu_user_time_max,cpu_user_time_total,cpu_sys_time_max,cpu_sys_time_total $sql = "update {$this->report_monitor} set fun_count=:fun_count,v6=:v6, total_diff_time=:total_diff_time,\n\t\t\t\t\t\t\t\t\tmemory_max=:memory_max, memory_total=:memory_total, cpu_user_time_max=:cpu_user_time_max, cpu_user_time_total=:cpu_user_time_total, cpu_sys_time_max=:cpu_sys_time_max, cpu_sys_time_total=:cpu_sys_time_total where md5=:md5 "; } else { $sql = "update {$this->report_monitor} set fun_count=fun_count+:fun_count,v6=:v6, total_diff_time=:total_diff_time,\n\t\t\t\t\t\t\t\t memory_max=:memory_max, memory_total=:memory_total, cpu_user_time_max=:cpu_user_time_max, cpu_user_time_total=:cpu_user_time_total, cpu_sys_time_max=:cpu_sys_time_max, cpu_sys_time_total=:cpu_sys_time_total where md5=:md5 "; } $stmt = _ociparse($conn_db, $sql); _ocibindbyname($stmt, ':md5', md5($time . $type . $host . $act . $key . $hostip)); _ocibindbyname($stmt, ':fun_count', $v['count']); _ocibindbyname($stmt, ':v6', abs($v['diff_time'])); _ocibindbyname($stmt, ':total_diff_time', $v['total_diff_time']); _ocibindbyname($stmt, ':memory_max', $v['memory_max']); _ocibindbyname($stmt, ':memory_total', $v['memory_total']); _ocibindbyname($stmt, ':cpu_user_time_max', $v['cpu_user_time_max']); _ocibindbyname($stmt, ':cpu_user_time_total', $v['cpu_user_time_total']); _ocibindbyname($stmt, ':cpu_sys_time_max', $v['cpu_sys_time_max']); _ocibindbyname($stmt, ':cpu_sys_time_total', $v['cpu_sys_time_total']); $oci_error = _ociexecute($stmt); print_r($oci_error); if ($oci_error) { _status(1, VHOST . "(BUG错误)", 'SQL错误', "{$get_included_files}/{$_GET['act']}", var_export(array('cal_date' => $time, 'v1' => $type, 'v2' => $host, 'v3' => $act, 'v4' => $key, 'v5' => $hostip, 'fun_count' => $v['count'], 'v6' => abs($v['diff_time']), 'total_diff_time' => $v['total_diff_time'], 'memory_max' => $v['memory_max'], 'memory_total' => $v['memory_total'], 'cpu_user_time_max' => $v['cpu_user_time_max'], 'cpu_user_time_total' => $v['cpu_user_time_total'], 'cpu_sys_time_max' => $v['cpu_sys_time_max'], 'cpu_sys_time_total' => $v['cpu_sys_time_total']), true) . "|" . var_export($oci_error, true), VIP); } else { _status(1, VHOST . "(PHPAPM)", "统计消耗", $type, 'monitor(update)', VIP); } $_row_count = ocirowcount($stmt); if (!$_row_count) { $xxi++; echo "{$xxi}:[{$time} . {$type} . {$host} . {$act} . {$key} . {$hostip}]\n"; $sql = "insert into {$this->report_monitor} (id,v1,v2,v3,v4,v5,fun_count,cal_date,v6,total_diff_time,memory_max,memory_total, cpu_user_time_max,cpu_user_time_total,cpu_sys_time_max,cpu_sys_time_total,md5)\n values(seq_{$this->report_monitor}.nextval,:v1,:v2,:v3,:v4,:v5,:fun_count,to_date(:cal_date,'yyyy-mm-dd hh24:mi:ss'),:v6,:total_diff_time,:memory_max,:memory_total, :cpu_user_time_max,:cpu_user_time_total,:cpu_sys_time_max,:cpu_sys_time_total,:md5)"; $stmt = _ociparse($conn_db, $sql); _ocibindbyname($stmt, ':md5', md5($time . $type . $host . $act . $key . $hostip)); _ocibindbyname($stmt, ':cal_date', $time); _ocibindbyname($stmt, ':v1', $type); _ocibindbyname($stmt, ':v2', $host); _ocibindbyname($stmt, ':v3', $act); _ocibindbyname($stmt, ':v4', $key); _ocibindbyname($stmt, ':v5', $hostip); _ocibindbyname($stmt, ':fun_count', $v['count']); _ocibindbyname($stmt, ':v6', abs($v['diff_time'])); _ocibindbyname($stmt, ':total_diff_time', $v['total_diff_time']); _ocibindbyname($stmt, ':memory_max', $v['memory_max']); _ocibindbyname($stmt, ':memory_total', $v['memory_total']); _ocibindbyname($stmt, ':cpu_user_time_max', $v['cpu_user_time_max']); _ocibindbyname($stmt, ':cpu_user_time_total', $v['cpu_user_time_total']); _ocibindbyname($stmt, ':cpu_sys_time_max', $v['cpu_sys_time_max']); _ocibindbyname($stmt, ':cpu_sys_time_total', $v['cpu_sys_time_total']); $oci_error = _ociexecute($stmt); print_r($oci_error); if ($oci_error) { _status(1, VHOST . "(BUG错误)", 'SQL错误', "{$get_included_files}/{$_GET['act']}", var_export(array('cal_date' => $time, 'time' => date('Y-m-d H:i:s'), 'md5' => md5($time . $type . $host . $act . $key . $hostip), 'v1' => $type, 'v2' => $host, 'v3' => $act, 'v4' => $key, 'v5' => $hostip, 'fun_count' => $v['count'], 'v6' => abs($v['diff_time']), 'memory_max' => $v['memory_max'], 'memory_total' => $v['memory_total'], 'cpu_user_time_max' => $v['cpu_user_time_max'], 'cpu_user_time_total' => $v['cpu_user_time_total'], 'cpu_sys_time_max' => $v['cpu_sys_time_max'], 'cpu_sys_time_total' => $v['cpu_sys_time_total']), true) . "|" . var_export($oci_error, true), VIP); } else { _status(1, VHOST . "(PHPAPM)", "统计消耗", $type, 'monitor', VIP); } } } } } } } } _ocilogoff($conn_db); if (!is_writable('/dev/shm')) { exit('no writable shm'); } if (!file_exists($dir = '/dev/shm/' . VHOST . '/')) { mkdir($dir); } if (!file_exists($dir1 = '/dev/shm/xss_' . VHOST . '/')) { mkdir($dir1); } include PHPAPM_PATH . "./lib/project_function.php"; $project_function = new project_function(); $check_files = array(); if (date('H') > 8 && date('H') <= 19) { $time_area = '白天'; } else { $time_area = '晚上'; } //文件记录 foreach ($files as $module_name => $_files) { foreach (array_unique($_files) as $file) { if (!is_file($file)) { continue; } //文件修改时间 $new_file = $dir . md5($file); // if (is_file($new_file) && filectime($new_file) < filectime($file)) { echo "代码改动\n"; _status(1, $module_name . "(代码改动)", "文件改动-{$time_area}", $file, "", VIP, 0); touch($new_file, filectime($file)); } elseif (!is_file($new_file)) { _status(1, $module_name . "(代码改动)", "新增文件-{$time_area}", $file, "", VIP, 0); touch($new_file, filectime($file)); } //安全校验 $new_file = $dir1 . md5($file); if (is_file($new_file) && filectime($new_file) < filectime($file)) { $check_files[$file] = $module_name; } elseif (!is_file($new_file)) { $check_files[$file] = $module_name; } } } foreach ($check_files as $file => $module_name) { $token = token_get_all(file_get_contents($file)); //代码所有人统计 if (strpos($file, '/phpCas/') === false || strpos($file, '/PHPMailer/') === false) { $project_function->_function_author($token, $module_name, $file); $project_function->_function_count($token, $module_name, $file); $project_function->_xss($token, $module_name, $file); $project_function->_sign($token, $module_name, $file); $project_function->_disable_function($token, $module_name, $file); } touch($dir1 . md5($file), filectime($file)); } die("\n" . date("Y-m-d H:i:s") . ',file:' . __FILE__ . ',line:' . __LINE__ . "\n"); }
/** * @desc WHAT? * @author xing39393939@gmail.com * @since 2012-07-03 16:08:37 * @throws 注意:无DB异常处理 */ function close() { if (is_object($this->memcacheObj) && method_exists($this->memcacheObj, 'close')) { $this->memcacheObj->close(); _status(1, VHOST . '(Memcahe连接)', "{$this->current_host['host']}:{$this->current_host['port']}[关闭]", GET_INCLUDED_FILES); $_SERVER['memcache_server']["{$this->current_host['host']}:{$this->current_host['port']}"] = $this->memcacheObj = null; unset($this->memcacheObj, $_SERVER['memcache_server']["{$this->current_host['host']}:{$this->current_host['port']}"]); } }
function _initialize() { ini_set("display_errors", true); $replace_date = date('H') . ':' . floor(date('i') / 10) * 10; exec("uptime", $uptime); //获取系统负载 exec('cat /proc/sys/kernel/hostname', $hostname); //获取服务器 print_r($hostname); $_POST['hostname'] = $hostname[0]; //系统负载 preg_match('#load average: ([0-9|.]+),#iUs', $uptime[0], $out); print_r($out); _status(round($out[1], 2), VHOST . '(WEB日志分析)', 'Load', $_POST['hostname'], $replace_date, VIP, 0, 'replace'); //IO压力 $io = NULL; $io = exec("top -b -n 1 | awk 'NR==3 {print \$6}' | awk -F '%' '{print \$1}'"); //mem echo "IO\n"; print_r($io); _status($io, VHOST . '(WEB日志分析)', 'IO', $_POST['hostname'], date('Y-m-d H'), VIP, 0, 'replace'); //运行时间 preg_match('#up ([0-9]+) day#iUs', $uptime[0], $out); echo "运行时间\n"; print_r($out); _status($out[1], VHOST . '(WEB日志分析)', '运行天数', $_POST['hostname'], date('Y-m-d H'), VIP, 0, 'replace'); //监控内存剩余 exec("cat /proc/meminfo | head -2 | tail -1", $mem); //mem print_r($mem); preg_match('#.*([0-9]+) KB#iUs', $mem[0], $out); _status(round($out[1] / 1024, 2), VHOST . '(WEB日志分析)', 'Mem内存剩余', $_POST['hostname'], $replace_date, VIP, 0, 'replace'); //CPU监控 exec("top -b -n 1 | awk 'NR==3 {print \$5}'", $cpu); //cpuinfo print_r($cpu); $_POST['cpu'] = str_replace('%id,', '', $cpu[0]); $_POST['cpu'] = 100 - $_POST['cpu']; _status($_POST['cpu'], VHOST . '(WEB日志分析)', 'CPU', $_POST['hostname'], $_POST['cpu'] . '%-' . $replace_date, VIP, 0, 'replace'); //磁盘 exec("df -h | awk 'NR>1{print \$6,\$5}'", $disk); //disk print_r($disk); foreach ($disk as $row) { if (strlen(trim($row)) <= 0) { continue; } $tmp = explode(" ", $row); $mnt_name = $tmp[0]; $num = $tmp[1]; $num = str_replace('%', '', $num); _status($num, VHOST . '(WEB日志分析)', '磁盘', $_POST['hostname'] . '-' . $mnt_name, $row . '-' . $replace_date, VIP, 0, 'replace'); } //监控TCP连接数 /* exec("cat /dev/shm/cache_tcp | awk '{print $4}' | grep :80$ | wc -l", $web_link); //web_link连接数 print_r($web_link); _status($web_link[0], VHOST . '(WEB日志分析)', 'TCP连接', '80端口连接数', $replace_date, VIP, 0, 'replace'); exec("cat /dev/shm/cache_tcp | awk '{print $(NF-1)}' | grep :3306$ | grep -v ':ffff:' ", $mysql); //mysql_link连接数 print_r($mysql); foreach ($mysql as $v) { _status(1, VHOST . '(WEB日志分析)', 'TCP连接', 'Mysql连接数(' . $v . ')', $replace_date, VIP, 0, 'replace'); } exec("cat /dev/shm/cache_tcp | awk '{print $(NF-1)}' | grep :1521$ | grep -v ':ffff:' ", $oracle_link); //oracle连接数 print_r($oracle_link); foreach ($oracle_link as $k => $v) { _status(1, VHOST . '(WEB日志分析)', 'TCP连接', 'oracle连接数(' . $v . ')', $replace_date, VIP, 0, 'replace'); } exec("cat /dev/shm/cache_tcp | awk '{print $(NF-1)}' | grep :11[2|3]1[0-9]$ | grep -v ':ffff:' ", $memcache_link); //memcache_link连接数 print_r($memcache_link); foreach ($memcache_link as $v) { _status(1, VHOST . '(WEB日志分析)', 'TCP连接', 'Memcache连接数(' . $v . ')', $replace_date, VIP, 0, 'replace'); } exec("cat /dev/shm/cache_tcp | awk '{print $(NF-1)}' | grep -v :11[2|3]1[0-9]$ | grep -v :1521$ | grep -v :3306$ | grep -v :80$| grep -v ':ffff:' ", $other_link); foreach ($other_link as $v) { $v = substr($v, 0, count($v) - 7); _status(1, VHOST . '(WEB日志分析)', 'TCP连接', '其它连接数', $replace_date . ' ' . $v, VIP, 0, 'replace'); } */ }
/** * @desc WHAT? * @author * @since 2012-11-25 00:00:41 * @throws 注意:无DB异常处理 */ function _xss($token = array(), $module_name, $file) { $fixi = 0; $fix_array = array(); $open = false; foreach ($token as $k => $v) { if (is_array($v)) { $v[0] = token_name($v[0]); if (in_array($v[0], array('T_ECHO', 'T_PRINT', 'T_OPEN_TAG_WITH_ECHO', 'T_EXIT'))) { $open = true; } if ($open) { if ($v[0] == 'T_VARIABLE') { $fix_array[$fixi]['must'] = 1; } $fix_array[$fixi]['txt'] .= $v[1]; if (!$fix_array[$fixi]['line']) { $fix_array[$fixi]['line'] = $v[2]; } } // if ($open && $v[0] == 'T_CLOSE_TAG') { $open = false; $fixi++; } } else { if ($open) { $fix_array[$fixi]['txt'] .= $v; if ($v == ';') { $open = false; $fixi++; } } } } foreach ($fix_array as $k => $v) { if (!$v['must']) { unset($fix_array[$k]); } else { $v['txt'] = strtolower($v['txt']); foreach (array('strip_tags', 'htmlspecialchars', 'json_encode', 'floatval', 'intval', 'round', 'urlencode', 'rawurlencode', 'http_build_query', 'md5', 'date', 'count') as $kk => $vv) { if (strpos($v['txt'], $vv . "(") !== false) { unset($fix_array[$k]); break; } } } } foreach ($fix_array as $k => $v) { if (strpos($file, '/header_funtion.php') !== false || strpos($file, '/project') !== false) { _status(1, $module_name . "(安全BUG)", "XSS注入[项目]", $file, var_export($v, true), VIP); } else { _status(1, $module_name . "(安全BUG)", "XSS注入", $file, var_export($v, true), VIP); } } }
<?php include_once dirname(__FILE__) . '/ressources/class.main_cf.inc'; include_once dirname(__FILE__) . '/ressources/class.ldap.inc'; include_once dirname(__FILE__) . "/ressources/class.sockets.inc"; include_once dirname(__FILE__) . "/ressources/class.collectd.inc"; $user = new usersMenus(); if ($user->AsSystemAdministrator == false) { die; } if (isset($_GET["PopUp"])) { echo "<div id='main_collectd_config' style='background-color:#FFFFFF;margin-top:2px;'>" . mysql_main_switch() . "</div>"; exit; } if (isset($_GET["_status"])) { echo _status(); exit; } if (isset($_GET["main"])) { echo mysql_main_switch(); exit; } if (isset($_GET["instance_choose"])) { echo collectd_selector_instance(); exit; } if (isset($_GET["type_choose"])) { echo collectd_selector_type_data(); exit; } if (isset($_GET["GenerateGraph"])) {
/** * @desc 执行SQL查询语句 * @author * @since 2012-04-02 09:53:56 * @param resource $stmt 数据库句柄资源 * @return resource $error 错误信息 * @throws 无DB异常处理 */ function _ociexecute($stmt, $mode = OCI_COMMIT_ON_SUCCESS) { $last_oci_sql = $_SERVER['last_oci_sql']; $ADD_PROJECT = ADD_PROJECT; if (!is_resource($stmt)) { $debug_backtrace = debug_backtrace(); array_walk($debug_backtrace, create_function('&$v,$k', 'unset($v["function"],$v["args"]);')); _status(1, VHOST . "(BUG错误)", "SQL错误", GET_INCLUDED_FILES, "非资源\$stmt | " . var_export($_SERVER['last_oci_bindname'], true) . "|" . var_export($_GET, true) . "|" . $last_oci_sql . "|" . var_export($debug_backtrace, true)); } if (PROJECT_SQL === true) { $ADD_PROJECT = '[项目]'; } $_SERVER['oci_sql_ociexecute']++; $t1 = microtime(true); ociexecute($stmt, $mode); $diff_time = sprintf('%.5f', microtime(true) - $t1); //表格与函数关联 $sql_type = NULL; $v = _sql_table_txt($last_oci_sql, $sql_type); $out = array(); preg_match('# in(\\s+)?\\(#is', $last_oci_sql, $out); if ($out) { $last_oci_sql = substr($last_oci_sql, 0, stripos($last_oci_sql, ' in')) . ' in....'; _status(1, VHOST . "(BUG错误)", '问题SQL', "IN语法" . $ADD_PROJECT, "{$_SERVER['last_db_conn']}@" . GET_INCLUDED_FILES . "/{$_REQUEST['act']}", "{$last_oci_sql}"); } _status(1, VHOST . '(SQL统计)' . $ADD_PROJECT, "{$_SERVER['last_db_conn']}{$sql_type}", strtolower($v) . "@" . GET_INCLUDED_FILES, $last_oci_sql, VIP, $diff_time); $diff_time_str = _debugtime($diff_time); if ($diff_time < 1) { _status(1, VHOST . '(SQL统计)', '一秒内', _debugtime($diff_time), "{$_SERVER['last_db_conn']}." . strtolower($v) . "@" . GET_INCLUDED_FILES . VIP, $last_oci_sql, $diff_time); } else { _status(1, VHOST . '(SQL统计)', '超时', _debugtime($diff_time), "{$_SERVER['last_db_conn']}." . strtolower($v) . "@" . GET_INCLUDED_FILES . VIP, $last_oci_sql, $diff_time); } $ocierror = ocierror($stmt); if ($ocierror) { $debug_backtrace = debug_backtrace(); array_walk($debug_backtrace, create_function('&$v,$k', 'unset($v["function"],$v["args"]);')); _status(1, VHOST . "(BUG错误)", "SQL错误", GET_INCLUDED_FILES, var_export($ocierror, true) . '|' . var_export($_SERVER['last_oci_bindname'], true) . "|GET:" . var_export($_GET, true) . '|POST:' . var_export($_POST, true) . "|" . $last_oci_sql . "|" . var_export($debug_backtrace, true), VIP, $diff_time); } $_SERVER['last_oci_bindname'] = array(); return $ocierror; }
function _web_log($log_file) { echo $log_file, "\n"; $total = exec("cat " . $log_file . " | wc -l"); echo "\n line:" . $total; $status_arr = array(200, 206, 301, 302, 304, 400, 403, 404, 405, 408, 413, 417, 499, 500, 501, 502, 504); $count_arr = array(); $count_sum = 0; foreach ($status_arr as $key => $status_code) { $cmd = "cat " . $log_file . " | grep '\" " . $status_code . " ' | wc -l"; echo $cmd . "\n"; $count_arr[$status_code] = exec($cmd); $count_sum += $count_arr[$status_code]; $error_ips_str = ""; if ($status_code >= 500) { $ip_cmd = "cat {$log_file} | grep '\" " . $status_code . " ' | awk '{print \$7}' | awk 'BEGIN { FS=\"?\" } {print \$1}' | uniq"; $error_ips = array(); exec($ip_cmd, $error_ips); $error_ips = array_count_values($error_ips); foreach ($error_ips as $ip => $count) { # code... $error_ips_str .= "{$ip}({$count})\n"; } } _status($count_arr[$status_code], VHOST . '(WEB日志分析)', $status_code, $status_code, $error_ips_str, VIP, 0, NULL, strtotime('-1 hours')); } _status($total - $count_sum, VHOST . '(WEB日志分析)', "其它", "其它", $error_ips_str, VIP, 0, NULL, strtotime('-1 hours')); //ip 统计 $cmd_ip_stats = "cat {$log_file} | awk '(\$9~/20|30/){print}' | awk '{print \$1}' | sort -n| uniq -c | sort -r"; $ip_stats = array(); exec($cmd_ip_stats, $ip_stats); _status(count($ip_stats), VHOST . '(WEB日志分析)', '独立ip', '独立ip', null, VIP, 0, NULL, strtotime('-1 hours')); for ($i = 0; $i < 10; $i++) { if (isset($ip_stats[$i])) { list($count, $ip) = explode(" ", trim($ip_stats[$i]), 2); //小于100的ip不进行统计 if ($count < 100) { break; } _status($count, VHOST . '(WEB日志分析)', 'ip统计前十', $ip, null, VIP, 0, NULL, strtotime('-1 hours')); } } //QPS统计 $qps_stats = exec("cat {$log_file} | awk '{print \$4}' | sort |uniq -c | sort -n -r | head -n1 | awk '{print \$1}'"); //日志明文处理完毕之后,压缩回去 $base_name_log_file = basename($log_file); chdir("/home/webid/logs/"); exec("tar -czvf {$base_name_log_file}.tar.gz {$base_name_log_file}"); //日志只保留zip压缩文件 if (is_file("{$base_name_log_file}.tar.gz")) { unlink($base_name_log_file); } return $qps_stats; }
function _assign($name, $value) { global $_assigns; _status("assigning '{$value}' to '{$name}'"); return $_assigns[$name] = $value; }
function _initialize() { //暂时关闭 exit; #每小时执行一次 if (date('i') != 30) { exit; } if (is_writable('/dev/shm/')) { $change = false; $basefile = '/dev/shm/sql_' . VHOST; $sqls = unserialize(file_get_contents($basefile)); if (empty($sqls)) { echo "empty sqls\n"; $change = true; } echo "sql_count:" . count($sqls) . "\n"; foreach ($sqls as $k => $v) { if ($v['type'] != 'oci' || $v['paser_txt'] || $v['vhost'] != VHOST) { continue; } if (strpos($v['sql'], 'alter session') !== false) { continue; } $conn_db = _ocilogon($v['db']); $sql = "EXPLAIN PLAN SET STATEMENT_ID='pps' FOR " . $v['sql']; $stmt = _ociparse($conn_db, $sql); _ociexecute($stmt); $sql = "SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY('PLAN_TABLE','pps','BASIC'))"; $stmt = _ociparse($conn_db, $sql); _ociexecute($stmt); $_row = array(); $row_text = NULL; while (ocifetchinto($stmt, $_row, OCI_ASSOC + OCI_RETURN_LOBS + OCI_RETURN_NULLS)) { echo "change:explain\n"; $change = true; $row_text .= "\n" . $_row['PLAN_TABLE_OUTPUT']; } _ocilogoff($conn_db); $sqls[$k]['paser_txt'] = $row_text; $sql_type = NULL; $vv = _sql_table_txt($v['sql'], $sql_type); // $type = NULL; if (strpos($v['act'], 'project') !== false) { $type = "(项目)"; } if (strpos($row_text, 'TABLE ACCESS FULL') !== false) { _status(1, VHOST . "(BUG错误)", "问题SQL", "全表扫描{$type}", "{$v['db']}.{$vv}@{$v['act']}", $v['sql'] . "\n" . $row_text); } if (strpos($row_text, ' JOIN ') !== false) { _status(1, VHOST . "(BUG错误)", "问题SQL", "多表查询{$type}", "{$v['db']}.{$vv}@{$v['act']}", $v['sql'] . "\n" . $row_text); } } foreach ($sqls as $k => $v) { if (time() > strtotime($v['add_time']) + 3600) { echo "change:time\n"; $change = true; unset($sqls[$k]); } } if ($change) { echo "write file.\n"; file_put_contents($basefile, serialize($sqls)); } die("OK\n"); } }
function vkPost($url, $message = 'message', $title = 'title', $descr = 'descr') { global $url_vk, $url_site; $o = '../cache/cookie.txt'; $h = _params($o, $url_vk, true); if ($h['my_id'] == 0) { _auth($o, $d, true); $h = _params($o, $url_vk, true); } if ($h['my_id'] != 0) { $r = _status($o, $h['post_hash'], $url, $message, $title, $descr, $h['user_id']); $c = preg_match_all('/page_wall_count_all/smi', $r, $f); if ($c == 0) { return false; } else { return true; } } }
/** * @desc WHAT? * @author * @since 2012-06-17 23:04:10 * @throws 注意:无Db异常处理 */ function _curl(&$chinfo, $url, $post_data = null, $config = array(), $upload_file = array()) { settype($config, 'array'); $ch = curl_init(); $chinfo = array(); if (substr($url, 0, 5) == 'https') { curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); } curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 5); curl_setopt($ch, CURLOPT_ENCODING, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:', 'Accept-Encoding:gzip,deflate,sdch', 'User-Agent:Mozilla/5.0 (Windows NT 5.1; rv:2.0) Gecko/20100101 Firefox/4.0' . trim(GET_INCLUDED_FILES), "Referer:{$url}")); foreach ($config as $k => $v) { curl_setopt($ch, $k, $v); } if ($post_data) { if (function_exists('http_build_query')) { curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data)); } else { curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); } } if ($upload_file) { curl_setopt($ch, CURLOPT_POSTFIELDS, (array) $upload_file + (array) $post_data); } $curl_error_tmp = $curl_error = NULL; $total_time = $i = 0; while (!$chinfo['http_code'] && $i <= 0) { $curl_data = curl_exec($ch); $curl_error_tmp = curl_error($ch); if ($curl_error_tmp) { $curl_error = $curl_error_tmp; } $chinfo = curl_getinfo($ch); $i++; $total_time += $chinfo['total_time']; } curl_close($ch); $url_path = explode('?', $chinfo['url']); $_SERVER['last_curl_info'][$url_path[0]] = $chinfo['url']; $_SERVER['last_curl_info_num']++; $chinfo['total_time'] = $total_time; $url_arr = parse_url($url); $url_arr_list = explode('.', $url_arr['host']); $url_arr_list_str = $url_arr_list[count($url_arr_list) - 2] . '.' . $url_arr_list[count($url_arr_list) - 1]; $debug_backtrace_str = NULL; foreach (debug_backtrace() as $vv) { $debug_backtrace_str .= "line:({$vv['line']}){$vv['function']}@file:{$vv['file']}\n"; } // if ($chinfo['http_code'] != '200' && $chinfo['http_code'][0] != '3') { _status(1, VHOST . '(BUG错误)', "网址抓取", "{$url_arr['host']}{$url_arr['path']}({$chinfo['http_code']})err:" . $curl_error, GET_INCLUDED_FILES . "\n{$chinfo['url']}\n{$debug_backtrace_str}", VIP, $total_time); } else { _status(1, VHOST . '(网址抓取)', $url_arr_list_str, "{$url_arr['host']}/{$url_arr['path']} ({$chinfo['http_code']})", GET_INCLUDED_FILES . "\n{$debug_backtrace_str}", VIP, $total_time); } //超时错误记录 if ($total_time < 1) { _status(1, VHOST . '(网址抓取)', '一秒内', _debugtime($total_time), $url_arr['host'] . "{$url_arr['path']} ({$chinfo['http_code']})" . $curl_error, GET_INCLUDED_FILES . "\n{$debug_backtrace_str}", $total_time); } else { _status(1, VHOST . '(网址抓取)', '超时', _debugtime($total_time), $url_arr['host'] . "{$url_arr['path']} ({$chinfo['http_code']})" . $curl_error, GET_INCLUDED_FILES . "\n{$debug_backtrace_str}", $total_time); } return $curl_data; }