背景
(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 | | +----------+--------------+------+-----+---------+----------------+
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。