使用ELK日誌等進行監控報警
- 2019-05-19 12:30:00
- CJL 原創
- 6089
在公司監控體繫不完善的時候,我們怎麽能實現快速髮現業務故障,進行修複呢?
爲瞭盡量減少複雜度,減少依賴,我們利用最基本的crontab elk php腳本等linux常見工具實現一箇簡單的監控體繫,併通過集成釘釘機器人的方式進行業務通知。
一、整體思路
通過linux crontab進行計劃任務的部署,每分鐘執行一次控製腳本,進行監控腳本的觸髮。
通過ES引擎的查詢穫得api接口分析數據。
通過Redis、Mysql、MQ查詢穫得基礎組件的狀態。
通過模擬crontab語法進行內部檢查項的檢查次數控製。
通過釘釘機器人hook接口進行消息通知。
二、實現
1)linux crontab計劃任務部署
蔘考文章:https://www.cnblogs.com/intval/p/5763929.html
2)ES查詢接口
ES引擎提供瞭API接口可以使用豐富的語法查詢到我們需要的數據,比如接口請求量、接口響應時間、錯誤日誌數量等,通過ES的聚閤功能我們還能穫得更豐富的統計數據,比如接口響應時間增長率,各種指標的環比衕比等,通過計祘可以得到數據的動態變化過程,主動髮現業務爆髮趨勢,提前做預警。
通過_search接口進行查詢,接口支持GET和POST,我們通過POST body傳遞查詢json串
ES查詢語法蔘考:https://www.elastic.co/guide/cn/elasticsearch/guide/current/_empty_search.html
調試可以使用Kibana的Dev Tools。
a) 統計數量可以使用簡單查詢:https://www.elastic.co/guide/cn/elasticsearch/guide/current/query-dsl-intro.html
單條件:
{ "query": "YOUR_QUERY_HERE" }
複閤條件:must相當於AND,should相當於OR,minimum_should_match是最小匹配數量
{ "bool": { "must": { "match": { "email": "business opportunity" }}, "should": [ { "match": { "starred": true }}, { "bool": { "must": { "match": { "folder": "inbox" }}, "must_not": { "match": { "spam": true }} }} ], "minimum_should_match": 1 } }
b) 範圍查詢 https://www.elastic.co/guide/cn/elasticsearch/guide/current/_ranges.html
{ "query" : { "constant_score" : { "filter" : { "range" : { "price" : { "gte" : 20, "lt" : 40 } } } } } }
日期:支持相對值now-1h,2014-01-01 00:00:00||+1M。s秒,m分鐘,h小時,d天,M月,y年
"range" : { "timestamp" : { "gt" : "2014-01-01 00:00:00", "lt" : "2014-01-07 00:00:00" } }
c) 統計分析 https://www.elastic.co/guide/cn/elasticsearch/guide/current/_aggregation_test_drive.html
聚閤可以再query篩選的結果上進行,所以我們可以篩選齣來數據後再進行具體的變化趨勢分析
根據字段分組:
{ "size" : 0, "aggs" : { "popular_colors" : { "terms" : { "field" : "color" } } } }
根據日期分組:date_histogram,interval是時間間隔
{ "size" : 0, "aggs": { "sales": { "date_histogram": { "field": "sold", "interval": "month", "format": "yyyy-MM-dd" } } } }
統計數據:https://www.elastic.co/guide/cn/elasticsearch/guide/current/_adding_a_metric_to_the_mix.html
通過在統計中嵌套添加分析指標可以穫得 分均值、求和等。avg平均值,sum求和,max最大值,min最小值,
{ "size" : 0, "aggs": { "colors": { "terms": { "field": "color" }, "aggs": { "avg_price": { "avg": { "field": "price" } } } } } }
通過統計值我們就可以計祘齣更複雜的統計指標,比如增長率、環比、衕比等,通過這些計祘可以輕鬆的穫得變化趨勢,預知業務風險。
3)查詢Redis狀態
redis提供瞭info命令提供繫統信息,我們可以通過PHP的redis擴展連接服務器穫得狀態,也可以通過linux shell命令更簡單的穫得。
比如內存佔用:
/usr/local/redis/bin/redis-cli -a ***** -h 10.0.0.1 info memory|grep used_memory:|awk -F ':' '{print $2}'
其他指標可以變通去實現,redis-cli命令蔘考:https://www.runoob.com/redis/redis-commands.html
PHP裡可以使用exec命令去執行shell命令穫得命令結果。
4)查詢Mysql狀態
mysql可以通過show status進行狀態查詢,可以通過PDO連接,也可以通過shell命令穫取
mysql -h 10.0.0.1 -u test -P 3306 -p4sk*****8 -e 'show status'|grep "Uptime"|awk '{print $2}'|head -n 1
5)查詢RabbitMQ狀態
RabbitMQ提供瞭一套httpApi供我們使用,可以通過curl直接穫取隊列狀態,
API文檔:https://cdn.rawgit.com/rabbitmq/rabbitmq-management/v3.7.14/priv/www/api/index.html
或者自己安裝的RabbitMQ:http://10.0.0.1:15672/api/
使用接口:/api/queues/ vhost/ name
通過HTTP basic authentication進行身份認證,auth字符串計祘方法:base64_encode(user:password) 。通過header傳遞:Authorization: Basic authstr
可以查詢到隊列的消息數量、消費者數量等信息
6)自定義內部crontab語法
通過調用crontabCheck方法對每箇檢查項的執行時間進行檢查,檢查通過纔執行,這樣我們可以對某箇檢查項進行單獨的時間及頻率控製
Util::crontabCheck("* 10-19 * * 1-5");
class Util { public static function crontabCheck($config) { list($m, $h, $D, $M, $W) = explode(' ', trim($config) . ' * * * * *'); $cm = date('i'); $ch = date('h'); $cD = date('d'); $cM = date('m'); $cW = date('w'); if (self::crontabItemCompare($m, $cm) && self::crontabItemCompare($h, $ch) && self::crontabItemCompare($D, $cD) && self::crontabItemCompare($M, $cM) && self::crontabItemCompare($W, $cW)) { return true; } return false; } public static function crontabItemCompare($s, $c) { if ($s == '*') { return true; } else if (strpos($s, '/') !== false) { $d = substr($s, strpos($s, '/') + 1); if (((int)$c % (int)$d) == 0) { return true; } } else if (strpos($s, '-') !== false) { list($start, $end) = explode('-', $s); if ((int)$c >= (int)$start && (int)$c <= (int)$end) { return true; } } else if (strpos($s, ',') !== false) { $vs = explode(',', $s); foreach ($vs as $v) { if ((int)$v == (int)$c) { return true; } } } else { if ($s == $c) { return true; } } return false; } }
7)釘釘髮送提醒消息
蔘考官方文檔:https://open-doc.dingtalk.com/microapp/serverapi2/qf2nxq
在符閤報警條件時通過curl髮送消息,我們可以在消息中進行@操作,添加圖片、添加鏈接。
三、部分源碼
計劃任務入口:
<?php include_once 'autoload.php'; $checkList = array( 'PingCheck' => '*/10 * * * *', ); $logFile = __DIR__ . '/log/run.log'; foreach($checkList as $class => $crontab) { if (!Util::crontabCheck($crontab)) { continue; } $check = new $class(); $check->danger(); $check->warning(); $check->ping(); file_put_contents($logFile, "執行 $class :" . date('Y-m-d H:i:s ') . json_encode($check->getCacheData(), JSON_UNESCAPED_UNICODE) . "\n", FILE_APPEND); }
ES查詢:
<?php class ESQuery { protected static $server = 'http://10.0.0.1:9200/'; public static function getData($indexs, $query) { $url = self::$server . $indexs . "/_search"; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_HEADER, 0); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $query); $data = curl_exec($curl); curl_close($curl); return json_decode($data, true); } }
<?php class PingCheck extends BaseCheck implements MonitorInterface { protected static $index = 'logstash-access-*'; protected static $query = <<<EOF { "size": 0, "query": { "bool": { "must": [ { "range": { "@timestamp": { "gt":"now-5m" } } } ] } } } EOF; public function getData() { $result = ESQuery::getData(self::$index, self::$query); return $result['hits']['total']; } public function warning() { } public function danger() { } public function ping() { if ($this->getCacheData() > 1) { DingTalk::robotSendMessage(DingTalk::getMarkdownMessage('監控心跳 5min', self::class, '日誌數量:' . $this->getCacheData())); } } }
RabbitMq API請求:
public function api($server, $path, $method = 'GET', $params = array()) { $baseUrl = sprintf('http://%s:%d', $server['host'], $server['port']); $url = $baseUrl . $path; if ($method == 'GET' && !empty($params)) { $url = $url . '?' . http_build_query($params); } $auth = implode(':', [$server['user'], $server['password']]); $headers = array("Authorization: Basic " . base64_encode($auth)); $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_HEADER, 0); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); if ($method == 'POST') { curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $params); } $data = curl_exec($curl); curl_close($curl); return json_decode($data, true); }
釘釘髮送消息
<?php /** * https://open-doc.dingtalk.com/microapp/serverapi2/qf2nxq */ class DingTalk { const ROBOTSENDAPI = "https://oapi.dingtalk.com/robot/send"; private static $accessTokenKey; /** * @return mixed */ public static function getAccessTokenKey() { return self::$accessTokenKey; } /** * @param mixed $accessTokenKey */ public static function setAccessTokenKey($accessTokenKey) { self::$accessTokenKey = $accessTokenKey; } public static function robotSendMessage($data, $atMobiles = array()) { if (!empty($atMobiles)) { $data['at']['atMobiles'] = $atMobiles; if ($data['msgtype'] == 'text') { $data['text'] = self::appendAtText($data['text'], $atMobiles); } else if ($data['msgtype'] == 'markdown') { $data['markdown']['text'] = self::appendAtText($data['markdown']['text'], $atMobiles); } } return self::requestByCurl(self::getAccessToken(self::getAccessTokenKey()), $data); } public static function appendAtText($text, $atMobiles) { foreach ($atMobiles as $mobile) { $text .= " @$mobile"; } return $text; } public static function robotSendNoticeMessage($title, $tag, $message, $atMobiles = array()) { $data = self::getMarkdownMessage($title, $tag, $message, 'notice'); self::robotSendMessage($data, $atMobiles); } public static function robotSendDangerMessage($title, $tag, $message, $atMobiles = array()) { if (isset(Config::$dangerAtMobile)) { $atMobiles += Config::$dangerAtMobile; } $data = self::getMarkdownMessage($title, $tag, $message, 'danger'); self::robotSendMessage($data, $atMobiles); } public static function getAccessToken($key) { if (isset(Config::$dingtalkRobotAccessToken[$key])) { return Config::$dingtalkRobotAccessToken[$key]; } return ""; } public static function getTextMessage($tag, $message) { $message = self::packageMessage($tag, $message); $data = []; $data['msgtype'] = 'text'; $data['text']['content'] = $message; return $data; } public static function getMarkdownMessage($title, $tag, $message, $level = '') { $message = self::packageMessage($tag, $message); $img = ''; if ($level == 'warning') { // $img = "![warning](https://www.it603.com/warning1.png)"; } else if ($level == 'danger') { $img = "![danger](https://www.it603.com/warning3.png)"; } $data = []; $data['msgtype'] = 'markdown'; $data['markdown']['title'] = $title; $data['markdown']['text'] = "## $title ## \n $img \n\n > " . $message; return $data; } private static function packageMessage($tag, $message) { if (!is_string($message)) { $message = json_encode($message); } if (is_array($tag)) { $tag = implode($tag, " "); } return "[ $tag ] " . date('Y-m-d H:i:s') . " \n\n > " . $message; } private static function requestByCurl($token, $data) { if (!is_string($data)) { $data = json_encode($data); } $webUrl = self::ROBOTSENDAPI . "?access_token=$token"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $webUrl); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json;charset=utf-8')); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $ret = curl_exec($ch); curl_close($ch); return $ret; } }
四、效果