Shell crawl Linux system metrics reported by api

  1. background

     (1) The system will encounter transport system run custom configured with multiple user-friendly, customized configuration monitoring belongs to the category of private monitoring platform configuration does not belong to the peacekeeping process, and personalized indicators unusual problem will cause the system to unexpected problems and the program runs the risk; for this scenario we will offer its own inspection system checks the script or agent to achieve the purpose of the protection system; the system detects the script is as follows;


  2. The system collects real script;

#!/bin/bash
# System Information crawl script
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}
be   
}
 
# 1. Acquisition system ip address
function getSysIp(){
ipaddr=$(/bin/hostname -i)
echo $ipaddr
}
 
# 2 dns resolve to check whether there is a valid 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"
    be
 
# 3. Acquisition system environment variable 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"
be
}
 
# 4 Get the name of the application
 
function getJavaName(){
appname=$(ps -ef |grep java |grep -v "grep"|head -1|awk -F "-D" '{print $2}'|awk -F "=" '{print $2}')
echo $appname
}
 
 
# 5 Get java application jvm parameters
function getJavaInfo(){
appinfo=$(ps -ef |grep java |grep -v "grep"|head -1|sed 's/[[:space:]]/-/g')
echo "$appinfo"
}
 
 
# 6 Get application port
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"
be
 
# 7. Get the current connection 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'
be
}
 
# 8 Check for 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"
    be
}
 
# 9. Dynamic invocation interface requests to send data to upload

function reqDynamicData(){
sysipaddr=$1
sysdns=$2
sysenv=$3
appName=$4
Appjwm = $ 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. Transmits data

function main(){
ip=`getSysIp`
dns=`getSysDns`
env=`getSysEnv`
name=`getJavaName`
jvm=`getJavaInfo`
port=`getAppPort`
tcp=`getTcp`
night = `sysNat`
reqDynamicData $ip $dns $env $name $jvm $port $tcp $nat
log=`init_logs`
runTime_date=`current_date`
echo "Upload Data Time: $ runTime_date" >> $ log    
}

main


3. Design a database field;

 3.1 to create a database 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 database table structure

+----------+--------------+------+-----+---------+----------------+
| 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    |                |
+----------+--------------+------+-----+---------+----------------+





Guess you like

Origin blog.51cto.com/breaklinux/2474269