/** * 初始化APP的数据 * * @param string $app 启用的app的名称,如:test */ private static function initApp($app) { if (empty($app)) { return FALSE; } Ym_Timer::startRecord(); define('YPP_APP', $app); define('YPP_APP_ROOT', YPP_DIR_APP . '/' . YPP_APP); define('YPP_APP_LIB', YPP_APP_ROOT . '/library'); define('YPP_APP_LOG', YPP_DIR_LOG . '/' . YPP_APP); // 声明loader,加载APP类文件 Yaf_Loader::getInstance(YPP_APP_LIB); // 初始化配置 Ym_Config::init(); $env = Ym_Config::getAppItem('env:monitor.env'); if ($env) { define('YPP_APP_ENV', $env); } else { define('YPP_APP_ENV', 'dev'); } // 初始化日志 if (YPP_APP_ENV == 'pro') { $logConf['levels'] = array('debug' => 1, 'warning' => 4, 'error' => 5, 'fatal' => 6, 'alert' => 7, 'emergency' => 8); } $logConf['logPath'] = YPP_APP_LOG; $logConf['logFile'] = YPP_APP; Ym_Logger::init($logConf); //Ym_Logger::info('test'); // 加载application.ini self::$app = new Yaf_Application(YPP_DIR_CONF . '/app/' . YPP_APP . '/application.ini'); }
public function extract(&$originData) { $returns = array(); $pattern = '/^(?<request_time>[^ ]*) (?<remote>[^ ]*) (?<host>[^ ]*) "(?<server>[^\\"]*)" "(?<server_status>[^\\"]*)" \\[(?<time>[^\\]]*)\\] "(?<method>[A-Z]*)? ?(?:(?<path>[^\\"]*) +\\S*)?" (?<status>[^ ]*) (?<size>[^ ]*)(?: "(?<referer>[^\\"]*)" "(?<agent>[^\\"]*)" "(?<forward>[^\\"]*)" (?<response_time>.*))?$/'; //var_dump(Ym_Config::getAppItem('monitor:parse.filter.host')); // $pattern = '/^(?P<request_time>[^ ]*) (?P<remote>[^ ]*) (?P<host>[^ ]*) "(?P<server>[^\"]*)" "(?P<server_status>[^\"]*)" \[(?P<time>[^\]]*)\] "(?P<method>\S+)(?: +(?P<path>[^\"]*) +\S*)?" (?P<status>[^ ]*) (?P<size>[^ ]*)(?: "(?P<referer>[^\"]*)" "(?P<agent>[^\"]*)" "(?P<forward>[^\"]*)" (?P<response_time>.*))?$/'; $filer = Ym_Config::getAppItem('monitor:parse.filter.host'); $filterHost = isset($filer) ? $filer : '-'; $filterHostArr = explode(',', $filterHost); foreach ($originData as $val) { preg_match($pattern, $val['message'], $matches); //var_dump($pattern, $val['message'], $matches); $path = strstr($matches['path'], '?', true); $matches['host'] = isset($matches['host']) ? $matches['host'] : ''; echo $matches['host'] . PHP_EOL; if (in_array($matches['host'], $filterHostArr)) { continue; } echo '-----', $matches['host'] . PHP_EOL; $matches['path'] = empty($path) ? $matches['path'] : $path; $matches['time'] = strtotime($matches['time']); // responseTime有时会出现用,分割的两个值 if (strpos($matches['response_time'], ',')) { $matches['response_time'] = strstr($matches['response_time'], ',', true); } $hostId = $this->getHostId($matches['host']); $pathId = $this->getPathId($hostId, $matches['host'], $matches['path']); $serverId = $this->getServerId($matches['server']); $data = array('request_time' => $matches['request_time'], 'remote' => $matches['remote'], 'host' => $matches['host'], 'time' => $matches['time'], 'method' => $matches['method'], 'path' => $matches['path'], 'status' => $matches['status'], 'size' => $matches['size'], 'referer' => $matches['referer'], 'agent' => $matches['agent'], 'forward' => $matches['forward'], 'response_time' => $matches['response_time'], 'server_id' => $serverId, 'server_status' => $matches['server_status'], 'host_id' => $hostId, 'path_id' => $pathId); $returns[] = $data; } return $returns; }
public function generateTdConfig() { if (!$this->configData) { return FALSE; } $config = Ym_Config::getAppItem('alarm:monitor'); $filename = isset($config['region']) ? $config['region'] : 'test'; $td_config = <<<EOF <match mongo.*> type mongo host 172.30.10.111 port 27017 database fluentd collection test tag_mapped remove_tag_prefix mongo. flush_interval 1s </match> <source> type tail path /usr/local/apache2/logs/access_log pos_file /tmp/access.log.pos tag mongo.access format none </source> EOF; file_put_contents('/tmp/td-agent.conf', $td_config); }
/** * 设置连接字符串 */ private function setConnectionString($name) { Ym_Config::init(); $config = Ym_Config::getAppItem('monitor'); $this->host = trim($config['mongo'][$name]['host']); $this->port = trim($config['mongo'][$name]['port']); $this->user = trim($config['mongo'][$name]['user']); $this->pass = trim($config['mongo'][$name]['pass']); $this->dbName = trim($config['mongo'][$name]['dbname']); $this->querySafety = trim($config['mongo'][$name]['query_safety']); $connectionString = "mongodb://"; if (empty($this->host)) { Sys_Common::showError("The Host must be set to connect to MongoDB", 'MongoDB'); } if (empty($this->dbName)) { Sys_Common::showError("The Database must be set to connect to MongoDB", 'MongoDB'); } if (!empty($this->user) && !empty($this->pass)) { $connectionString .= "{$this->user}:{$this->pass}"; } if (isset($this->port) && !empty($this->port)) { $connectionString .= "{$this->host}:{$this->port}"; } else { $connectionString .= "{$this->host}"; } $this->connectionString = trim($connectionString); }
public function init() { $classExtd = explode('_', __CLASS__); $className = strtolower($classExtd[1]); $this->actions = Ym_Config::getAppItem('actions:' . $className); // print_r($this->actions); // exit; }
public function _initConfig() { $this->config = Yaf_Application::app()->getConfig(); //把配置保存起来 $monitor = Ym_Config::getAppItem('monitor'); $actions = Ym_Config::getAppItem('actions'); $alarm = Ym_Config::getAppItem('alarm'); Yaf_Registry::set('monitor_config', $monitor); Yaf_Registry::set('actions_config', $actions); Yaf_Registry::set('alarm_config', $alarm); Yaf_Registry::set('common_config', Ym_Config::getAppItem('common')); }
public function run() { $alarmConfig = new Model_Alarm_Config(); $configData = array(); $id = $this->getRequest()->getQuery('id'); if ($id) { $configData = $alarmConfig->selectdata($id); } $configData['log'] = $alarmConfig->getlogConfig(); $configData['api'] = Ym_Config::getAppItem("actions:monitorscript"); $configData[0]['ouser'] = $alarmConfig->getMonitorUser(); $configData[0]['ogroup'] = $alarmConfig->getMonitorGroup(); $this->getView()->assign('L', $configData ? $configData : array()); }
public static function command($url = '', $asJob = TRUE, $return = TRUE) { $job = ""; $out = ">/dev/null 2>&1"; $phpcmd = "/bin/php"; $yafClient = Ym_Config::getAppItem("application:application.client"); $requestUri = "request_uri=" . $url; if ($asJob) { $job = "&"; } if ($return) { $out = ""; } $cmd = trim($phpcmd . " " . $yafClient . " '" . $requestUri . "' " . $out . " " . $job); $result = exec($cmd); return $result; }
public function getNodeData($hosts = '') { $returns = array(); $beginTime = time() - 60; $node_str = Ym_Config::getAppItem('monitor:lbs.node'); $node_arr = $node_str ? explode(',', $node_str) : array(201, 203, 205, 207, 209, 200, 0); $nodes = array(); $delay = 10; $len = 60; $end = strtotime(date('Y-m-d H:i', time() - $delay)); $from = $end - $len; //echo date('Y-m-d H:i',$from); foreach ($node_arr as $node) { $sql = "select sum(num) as num from log_hps where time>" . $from . " and time<=" . $end . " and node=" . $node; if ($hosts) { $sql = "select sum(num) as num from log_uhps where time>" . $from . " and time<=" . $end . " and node=" . $node . " and host_id in (" . $hosts . ")"; } $nums = $this->dao->queryRow($sql, TRUE); $nodes[$node] = isset($nums['num']) ? (int) $nums['num'] : 0; } return $nodes; //array(200=>0,201=>88311,203=>66022,205=>86922,207=>80858); }
/** * Get the full file * * @param string $fileName * @return string/bool */ protected static function getFile($index = '', $app = '') { $fileName = strpos($index, ':') ? trim(substr($index, 0, strrpos($index, ':')), '/') . '.ini' : $index . '.ini'; if (empty($index)) { $fileName = 'config.ini'; } if (!empty($app)) { $app = 'app/' . $app . DIRECTORY_SEPARATOR; } $file = self::$baseDir . $app . $fileName; if (file_exists($file)) { self::$fileIndex = $file; return $file; } return FALSE; }
/** * Yaf client command * * @author Xuexb<*****@*****.**> * @package xiaobo/sh * @since Version 1.0.1 @20140423 * @copyright Copyright (c) 2014, Yeahmobi, Inc. */ date_default_timezone_set('Asia/Shanghai'); define('YPP_APP', 'yeahmonitor'); define('YPP_APP_ROOT', YPP_DIR_APP . '/' . YPP_APP); define('YPP_APP_LIB', YPP_APP_ROOT . '/library'); // 声明loader Yaf_Loader::getInstance(YPP_APP_LIB, YPP_ROOT_PHPLIB); // 初始化配置和日志类 Ym_Config::init(); $logConf['logPath'] = YPP_DIR_LOG . '/app/' . YPP_APP; $logConf['logFile'] = YPP_APP; Ym_Logger::init($logConf); // 加载application.ini $app = new Yaf_Application(YPP_DIR_CONF . '/app/' . YPP_APP . '/application.ini'); $app->bootstrap(); $app->getDispatcher()->dispatch(new Yaf_Request_Simple()); //方法2:不试用带module的模式 //$controller = isset($argv[1]) ? $argv[1] : ''; //$action = isset($argv[2]) ? $argv[2] : ''; //if (!$controller || !$action) { // die('Please Use like this: /dianyi/app/ypp/bin/php yafClient.php controller action [argv]' . PHP_EOL); //} //$argv = isset($argv[3]) ? $argv[3] : ''; //$res = $app->execute(array('Controller_' . $controller, $action . 'Action'), $argv);
/** * * yafclient exec Add by zhangy * @param str $url * @param bool $asJob */ public function phpCli($url = '', $asJob = TRUE, $return = TRUE) { $job = ""; $out = ">/dev/null 2>&1"; $phpcmd = "/bin/php"; $yafClient = Ym_Config::getAppItem("application:application.client"); $requestUri = "request_uri=" . $url; if ($asJob) { $job = "&"; } if ($return) { $out = ""; } $cmd = trim($phpcmd . " " . $yafClient . " '" . $requestUri . "' " . $out . " " . $job); Ym_Logger::info($cmd); return system($cmd); }