-
Notifications
You must be signed in to change notification settings - Fork 10
/
proc.sh
66 lines (53 loc) · 2.33 KB
/
proc.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/bin/bash
logdir=$1
: ${logdir:=.}
# port=$1 #获取进程port
ports=(22 80 8080)
cmds=(client_bc redis java dockerd zookeeper kafka orderer peer nginx)
#获取进程pid
hostname=`hostname`
step=60
olddate=`date +%Y%m%d`
function send(){
tags=$1 # port=$port cmdline=$cmd
pid=$2
ts=`date +%s`
cpu=`ps --no-heading --pid=$pid -o pcpu|sed s/[[:space:]]//g` #获取cpu占用
ios=`cat /proc/$pid/io`
ioin=`echo "$ios"|grep read_bytes|awk '{print $2}'` #获取io输入
ioout=`echo "$ios"|grep -v cancelled_write_bytes|grep write_bytes|awk '{print $2}'` #获取io输出
mem=`cat /proc/$pid/status|grep -e VmRSS| awk '{print $2}'` #获取内存
if [ "$mem" == "" ];then
mem=0
fi
mem=$[ $mem * 1024 ]
metrics="[{\"endpoint\":\"$hostname\",\"metric\":\"proc.cpu\",\"value\":$cpu,\"step\":$step,\"counterType\":\"GAUGE\",\"timestamp\":$ts,\"tags\":\"${tags}\"}","{\"endpoint\":\"$hostname\",\"metric\":\"proc.mem\",\"value\":$mem,\"step\":$step,\"counterType\":\"GAUGE\",\"timestamp\":$ts,\"tags\":\"${tags}\"}","{\"endpoint\":\"$hostname\",\"metric\":\"proc.io.in\",\"value\":$ioin,\"step\":$step,\"counterType\":\"GAUGE\",\"timestamp\":$ts,\"tags\":\"${tags}\"}","{\"endpoint\":\"$hostname\",\"metric\":\"proc.io.out\",\"value\":$ioout,\"step\":$step,\"counterType\":\"GAUGE\",\"timestamp\":$ts,\"tags\":\"${tags}\"}]"
newdate=`date +%Y%m%d`
if [ $newdate != $olddate ];then
mv ${logdir}/proc.log ${logdir}/proc${olddate}.log
olddate=$newdate
fi
echo $metrics >> ${logdir}/proc.log
curl -X POST -d $metrics http://192.168.29.244:1988/v1/push
echo
}
while true; do
for p in ${ports[@]}; do
pid=`netstat -anp | grep ":$p " | grep LISTEN| awk '{print $7}' | awk -F"/" '{ print $1 }'|uniq`
if [ "$pid" != "" ]; then
echo port=$p $pid
send port=$p $pid
fi
done
for cmd in ${cmds[@]}; do
pids=`ps -ef|grep $cmd|grep -v docker-containerd|grep -v grep|awk '{print $2}'|tr -s '\n' ' '`
if [ "$pids" == "" ];then
continue
fi
for pid in ${pids[@]};do
echo pid=$pid,cmdline=$cmd "$pid"
send pid=$pid,cmdline=$cmd "$pid"
done
done
sleep $step
done