您好,登錄后才能下訂單哦!
背景
? ? ?(1).系統運維過程中會遇到 系統運行定制化跟多個人性配置,個性化配置屬于私有配置不屬于監控平臺的監控范疇,且個性化指標出現異常問題會導致系統出現不可預期問題及程序運行風險; 針對這種場景 我們會自己提供檢查系統檢查腳本或者agent 達到系統保障目的;? 如下就是系統探測腳本;
? 2.系統收集實戰腳本;
#!/bin/bash #系統信息抓取腳本 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.獲取系統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.獲取系統env環境變量 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參數 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.調用動態上傳接口發送請求數據 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.發送數據 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?"上傳數據時間:$runTime_date"?>>?$log???? } main
3.數據庫字段設計;
?3.1?創建數據庫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.數據庫表結構 +----------+--------------+------+-----+---------+----------------+ |?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????|????????????????| +----------+--------------+------+-----+---------+----------------+
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。