溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Shell 抓取Linux 系統(tǒng)指標通過api 上報

發(fā)布時間:2020-04-08 19:40:04 來源:網(wǎng)絡 閱讀:87 作者:breaklinux 欄目:系統(tǒng)運維
  1. 背景

? ? ?(1).系統(tǒng)運維過程中會遇到 系統(tǒng)運行定制化跟多個人性配置,個性化配置屬于私有配置不屬于監(jiān)控平臺的監(jiān)控范疇,且個性化指標出現(xiàn)異常問題會導致系統(tǒng)出現(xiàn)不可預期問題及程序運行風險; 針對這種場景 我們會自己提供檢查系統(tǒng)檢查腳本或者agent 達到系統(tǒng)保障目的;? 如下就是系統(tǒng)探測腳本;


? 2.系統(tǒng)收集實戰(zhàn)腳本;

#!/bin/bash
#系統(tǒng)信息抓取腳本
function?current_date(){
start_current_date="`date?"+%Y-%m-%d?%H:%M:%S"`"
echo?${start_current_date}
}
?
function?init_logs(){
log_dir="/chj/logs/"
log_file="/chj/logs/pushData.log"
if?[?-f?${log_file}?]
then
???echo?${log_file}
else
???touch?${log_file}???
???echo??${log_file}
fi???
}
?
#1.獲取系統(tǒng)ip地址
function?getSysIp(){
ipaddr=$(/bin/hostname?-i)
echo?$ipaddr
}
?
#2.解析dns檢查是否存在有效dns;
function?getSysDns(){
????domain="do.chj.cloud"
????if?dig?@172.21.2.10?$domain?+short?|grep?'[0-9]'?>/dev/null;then
????????echo?"True"
????else
????????echo?"False"
????fi
}?
?
#3.獲取系統(tǒng)env環(huán)境變量

function?getSysEnv(){
envfile=/etc/profile.d/chj-common.sh
if?[?-f?$envfile?]?
then
???env=$(grep?"RUNTIME_ENV="?$envfile?|awk?-F?"="?'{print?$2}')
???echo?$env
else
???echo?"none"
fi
}
?
#4.獲取應用名稱
?
function?getJavaName(){
appname=$(ps?-ef?|grep?java?|grep?-v?"grep"|head?-1|awk?-F?"-D"?'{print?$2}'|awk?-F?"="?'{print?$2}')
echo?$appname
}
?
?
#5.獲取java應用jvm參數(shù)
function?getJavaInfo(){
appinfo=$(ps?-ef?|grep?java?|grep?-v?"grep"|head?-1|sed?'s/[[:space:]]/-/g')
echo?"$appinfo"
}
?
?
#6.獲取應用端口
function?getAppPort(){
apppid=$(ps?-ef?|grep?java?|grep?-v?"grep"?|?/bin/awk?'{print?$2}'?|head?-1)
if?[?!?-z?"$apppid"?]
then
????appport=$(netstat?-tulpn?|?grep?"$apppid"?|?awk?-F?":"?'{print?$2}'|/bin/awk?-F?"?"?'{print?$1}')
????echo?"$appport"
?????
else
????echo?"none"
fi
}?
?
#7.獲取當前連接tcp
function?getTcp(){
tcp=$(netstat?-anplt??|grep?-v?tcp6?|grep?-v?"127.0.0.1"|grep?"ESTABLISHED"|sort?|awk?'!a[$5]++{print}'|awk?'{print?$5,$7}'|sed?'s/[[:space:]]/-/g'|sed?'s/\///g'|sed?'s/sshd:/sshd/'|sed?s'/-.-//g')
if?[?!?-n?"tcp"?]
then
?????echo?"none"
else
?????echo?$tcp|sed?'s/[[:space:]]/,/g'
fi
}
?
#8.檢查是否存在nat;

function?sysNat(){
????local?timeout="1"
????local?target="www.badu.com"
????local?ret_code=`curl?-I?-s?--connect-timeout?${timeout}?${target}?-w?%{http_code}?|?tail?-n1`
????if?[?"x$ret_code"?=?"x200"?];?then
????????echo?"1"
????else
????????echo?"0"
????fi
}
?
#9.調(diào)用動態(tài)上傳接口發(fā)送請求數(shù)據(jù)

function?reqDynamicData(){
sysipaddr=$1
sysdns=$2
sysenv=$3
appName=$4
appjvm=$5
appPort=$6
conntcp=$7
nat=$8
fetchUrl="http://ops.chj.cloud/dynamic/api/v1"
/bin/curl?"$fetchUrl"?-X?POST?-H?"Content-Type:?application/json"?-d?"{\"instance_ip\":?\"$sysipaddr\",\"env\":\"$sysenv\",\"dns\":\"$sysdns\",\"appname\":\"$ppName\",\"appjvm\":\"$appjvm\",\"appport\":\"$appPort\",\"apptcp\":\"$conntcp\",\"nat\":\"$nat\"}"
}

?
#10.發(fā)送數(shù)據(jù)

function?main(){
ip=`getSysIp`
dns=`getSysDns`
env=`getSysEnv`
name=`getJavaName`
jvm=`getJavaInfo`
port=`getAppPort`
tcp=`getTcp`
nat=`sysNat`
reqDynamicData?$ip?$dns?$env?$name?$jvm?$port?$tcp?$nat
log=`init_logs`
runTime_date=`current_date`
echo?"上傳數(shù)據(jù)時間:$runTime_date"?>>?$log????
}

main


3.數(shù)據(jù)庫字段設計;

?3.1?創(chuàng)建數(shù)據(jù)庫sql;
?
?CREATE?TABLE?`dynamic_Check`?(
??`id`?int(11)?NOT?NULL?AUTO_INCREMENT,
??`ip`?varchar(92)?COLLATE?utf8mb4_bin?DEFAULT?NULL,
??`dns`?varchar(128)?COLLATE?utf8mb4_bin?DEFAULT?NULL,
??`env`?varchar(64)?COLLATE?utf8mb4_bin?DEFAULT?NULL,
??`appname`?text?COLLATE?utf8mb4_bin,
??`appport`?varchar(32)?COLLATE?utf8mb4_bin?DEFAULT?NULL,
??`appjvm`?text?COLLATE?utf8mb4_bin,
??`conn_tcp`?text?COLLATE?utf8mb4_bin,
??`snat`?varchar(32)?COLLATE?utf8mb4_bin?DEFAULT?NULL,
??`run_time`?datetime?DEFAULT?NULL,
??PRIMARY?KEY?(`id`)
)?ENGINE=InnoDB?AUTO_INCREMENT=12?DEFAULT?CHARSET=utf8mb4?COLLATE=utf8mb4_bin?


3.2.數(shù)據(jù)庫表結構

+----------+--------------+------+-----+---------+----------------+
|?Field????|?Type?????????|?Null?|?Key?|?Default?|?Extra??????????|
+----------+--------------+------+-----+---------+----------------+
|?id???????|?int(11)??????|?NO???|?PRI?|?NULL????|?auto_increment?|
|?ip???????|?varchar(92)??|?YES??|?????|?NULL????|????????????????|
|?dns??????|?varchar(128)?|?YES??|?????|?NULL????|????????????????|
|?env??????|?varchar(64)??|?YES??|?????|?NULL????|????????????????|
|?appname??|?text?????????|?YES??|?????|?NULL????|????????????????|
|?appport??|?varchar(32)??|?YES??|?????|?NULL????|????????????????|
|?appjvm???|?text?????????|?YES??|?????|?NULL????|????????????????|
|?conn_tcp?|?text?????????|?YES??|?????|?NULL????|????????????????|
|?snat?????|?varchar(32)??|?YES??|?????|?NULL????|????????????????|
|?run_time?|?datetime?????|?YES??|?????|?NULL????|????????????????|
+----------+--------------+------+-----+---------+----------------+





向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI