Last active
November 7, 2018 15:09
-
-
Save Yxnt/2026c02bc10fd5d7db3250aa216d146d to your computer and use it in GitHub Desktop.
openfalcon bash agent centos5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Falcon Agent Bash | |
# https://www.cnblogs.com/pycode/p/6721373.html | |
endpoint="$hostname" | |
timestamp=`date +%s` | |
interval=30 | |
transfer="$gateway" | |
alive(){ | |
echo -e "{'endpoint':'$endpoint','metric':'agent.alive','value':1,'step':$interval,'counterType':'GAUGE','tags':'','timestamp':$timestamp}" | |
} | |
load(){ | |
# system load1、load5、load15 | |
# /proc/loadavg | |
load1_value=`awk '{print $1}' /proc/loadavg` | |
load5_value=`awk '{print $2}' /proc/loadavg` | |
load15_value=`awk '{print $3}' /proc/loadavg` | |
load1="{'endpoint':'$endpoint','metric':'load.1min','value':$load1_value,'step':$interval,'counterType':'GAUGE','tags':'','timestamp':$timestamp}" | |
load5="{'endpoint':'$endpoint','metric':'load.5min','value':$load5_value,'step':$interval,'counterType':'GAUGE','tags':'','timestamp':$timestamp}" | |
load15="{'endpoint':'$endpoint','metric':'load.15min','value':$load15_value,'step':$interval,'counterType':'GAUGE','tags':'','timestamp':$timestamp}" | |
data="$load1,$load5,$load15" | |
echo $data | |
} | |
cpu(){ | |
# cpu idle、busy、guest、iowait、irq、softirq、nice、steal、system、user、cnt、switches | |
# /proc/stat | |
echo -e `awk 'BEGIN{ | |
total=0 | |
} | |
{ | |
if (NR==1){ | |
for (i=2;i<=NF;i++){ | |
print $i | |
} | |
} | |
} | |
' /proc/stat` | |
} | |
cpuinfo(){ | |
# pre_cpu_time=`awk 'BEGIN{total=0}{for (i=1;i<=NF;i++){total=$i+total}print total}' cpu` | |
# pre_idle_time=`awk '{print $4}' cpu` | |
# pre_user_time=`awk '{print $1}' cpu` | |
# pre_system_time=`awk '{print $2}' cpu` | |
echo -e `awk ' | |
BEGIN{ | |
total=0 | |
} | |
{ | |
if (NR==1){ | |
for (i=2;i<=NF;i++){ | |
total = $i+total | |
} | |
idle=$5 / total * 100 | |
user=$2 / total * 100 | |
iowait=$4 / total * 100 | |
system_=$3/total * 100 | |
idle_metric="cpu.idle" | |
user_metric="cpu.user" | |
system_metric="cpu.system" | |
iowait_metric="cpu.iowait" | |
print "'{\'endpoint\':\'$endpoint\',\'metric\':\''"idle_metric"'\',\'value\':'"idle"',\'step\':$interval,\'counterType\':\'GAUGE\',\'tags\':\'\',\'timestamp\':$timestamp'},""'{\'endpoint\':\'$endpoint\',\'metric\':\''"user_metric"'\',\'value\':'"user"',\'step\':$interval,\'counterType\':\'GAUGE\',\'tags\':\'\',\'timestamp\':$timestamp'},""'{\'endpoint\':\'$endpoint\',\'metric\':\''"system_metric"'\',\'value\':'"system_"',\'step\':$interval,\'counterType\':\'GAUGE\',\'tags\':\'\',\'timestamp\':$timestamp'},""'{\'endpoint\':\'$endpoint\',\'metric\':\''"iowait_metric"'\',\'value\':'"iowait"',\'step\':$interval,\'counterType\':\'GAUGE\',\'tags\':\'\',\'timestamp\':$timestamp'}" | |
} | |
}' /proc/stat` | |
} | |
mem(){ | |
# memory free、used、total | |
# /proc/meminfo | |
echo $(awk 'BEGIN { | |
free=0; | |
total=0; | |
} | |
{ if (NR==1) { | |
total = $2*1024 | |
} else if (NR==2) { | |
free = $2*1024 | |
used = total-free | |
}} | |
END { | |
print "'{\'endpoint\':\'$endpoint\',\'metric\':\'mem.memtotal\',\'value\':'"total"',\'step\':$interval,\'counterType\':\'GAUGE\',\'tags\':\'\',\'timestamp\':$timestamp'},'{\'endpoint\':\'$endpoint\',\'metric\':\'mem.memfree\',\'value\':'"free"',\'step\':$interval,\'counterType\':\'GAUGE\',\'tags\':\'tag\',\'timestamp\':$timestamp'},'{\'endpoint\':\'$endpoint\',\'metric\':\'mem.memused\',\'value\':'"used"',\'step\':$interval,\'counterType\':\'GAUGE\',\'tags\':\'tag\',\'timestamp\':$timestamp'}" | |
}' /proc/meminfo) | |
} | |
disk_info_save(){ | |
# 初始值 | |
df |sed '1d;/ /!N;s/\n//;s/ \+/ /;'| awk ' | |
{ | |
len=split($1,disk,"/") | |
if (length(disk[3])!=0){ | |
sub(/[0-9]/,"",disk[3]) | |
command = "if [ -e /sys/block/"disk[3]"/stat ];then cat /sys/block/"disk[3]"/stat;fi" | |
command|getline cmd;close("cmd") | |
len=split(cmd,diskinfo," ") | |
if (length(diskinfo[3])!=0){ | |
print disk[3],cmd,"'$timestamp'" | |
} | |
} | |
}' | |
} | |
diskio(){ | |
# /proc/diskstats | |
# http://www.cnblogs.com/hbt19860104/p/3457929.html | |
# https://www.cnblogs.com/zk47/p/4733143.html | |
echo -e $(awk 'BEGIN{ | |
get_file_line="sed -n '$=' disk" | |
get_file_line|getline linenumber;close(linenumber) | |
} { | |
diskname=$1 | |
timestamp=$NF | |
command = "cat /sys/block/"diskname"/stat" | |
command|getline cmd;close(cmd) | |
len=split(cmd,diskinfo," ") | |
read_sectors=diskinfo[3] | |
write_sectors=diskinfo[7] | |
read=(read_sectors-$4)/("'$timestamp'"-timestamp) * 100 | |
write=(write_sectors-$8)/("'$timestamp'"-timestamp) * 100 | |
if (NR == 1){ # 第一行则清空文件 | |
mv_disk_tmp="mv disk{,.tmp}" | |
mv_disk_tmp|getline mv;close(mv) | |
} | |
output_file="echo "diskname" " cmd " " "'$timestamp'" " >> disk" | |
output_file|getline output;close(output) | |
w_metric="disk.io.write_bytes/device="diskname | |
r_metric="disk.io.read_bytes/device="diskname | |
print "'{\'endpoint\':\'$endpoint\',\'metric\':\''"r_metric"'\',\'value\':'"read"',\'step\':$interval,\'counterType\':\'GAUGE\',\'tags\':\'\',\'timestamp\':$timestamp'},""'{\'endpoint\':\'$endpoint\',\'metric\':\''"w_metric"'\',\'value\':'"write"',\'step\':$interval,\'counterType\':\'GAUGE\',\'tags\':\'\',\'timestamp\':$timestamp'}," | |
} | |
' disk) | |
} | |
diskbytes(){ | |
# disk bytes stats | |
echo -e `df -T|sed '1d;/ /!N;s/\n//;s/ \+/ /;' | awk 'BEGIN{} | |
{ | |
fstype=$2 | |
disk_total=$3 * 1024 | |
disk_used=$4 * 1024 | |
disk_free=$5 * 1024 | |
mount=$7 | |
if(disk_total==""){ | |
disk_total=0 | |
} | |
if (disk_used==""){ | |
disk_used=0 | |
} | |
if (disk_free==""){ | |
disk_free=0 | |
} | |
t_metric="df.bytes.total/fstype="fstype",mount="mount | |
f_metric="df.bytes.free/fstype="fstype",mount="mount | |
u_metric="df.bytes.used/fstype="fstype",mount="mount | |
print "'{\'endpoint\':\'$endpoint\',\'metric\':\''"t_metric"'\',\'value\':'"disk_total"',\'step\':$interval,\'counterType\':\'GAUGE\',\'tags\':\'\',\'timestamp\':$timestamp'},""'{\'endpoint\':\'$endpoint\',\'metric\':\''"f_metric"'\',\'value\':'"disk_free"',\'step\':$interval,\'counterType\':\'GAUGE\',\'tags\':\'\',\'timestamp\':$timestamp'},""'{\'endpoint\':\'$endpoint\',\'metric\':\''"u_metric"'\',\'value\':'"disk_used"',\'step\':$interval,\'counterType\':\'GAUGE\',\'tags\':\'\',\'timestamp\':$timestamp'}," | |
} | |
'` | |
} | |
network(){ | |
# TODO: 代码优化 | |
# network in:bytes、packets、errs、drop、fifo、frame、compressed、multicast | |
# network out:bytes、packets、errs、drop、fifo、colls、carrier、compressed | |
# /proc/net/dev | |
echo -e `awk 'BEGIN { | |
in_type="packets errs drop fifo.errs frame.errs compressed multicast" | |
out_type="bytes packets errs drop fifo.errs colls carrier.errs compressed" | |
in_len=split(in_type,in_arr," ") | |
out_len=split(out_type,out_arr," ") | |
data[0]=0 | |
str="" | |
} | |
{ | |
split($1,interface,":") | |
if (interface[1] ~ "eth" || interface[1] ~ "enp"){ | |
for (i=1;i<=in_len;i++){ | |
metric="net.if.in."in_arr[i]"/iface="interface[1] | |
value=$(i+1) | |
data[i]="'{\'endpoint\':\'$endpoint\',\'metric\':\''"metric"'\',\'value\':'"value"',\'step\':$interval,\'counterType\':\'DERIVE\',\'tags\':\'\',\'timestamp\':$timestamp'}" | |
} | |
for (i=1;i<=out_len;i++){ | |
metric="net.if.out."out_arr[i]"/iface="interface[1] | |
value=$(i+8) | |
data[(i+7)]="'{\'endpoint\':\'$endpoint\',\'metric\':\''"metric"'\',\'value\':'"value"',\'step\':$interval,\'counterType\':\'DERIVE\',\'tags\':\'\',\'timestamp\':$timestamp'}" | |
} | |
delete data[0] | |
in_metric="net.if.in.bytes/iface="interface[1] | |
value=interface[2] | |
data[16]="'{\'endpoint\':\'$endpoint\',\'metric\':\''"in_metric"'\',\'value\':'"value"',\'step\':$interval,\'counterType\':\'DERIVE\',\'tags\':\'\',\'timestamp\':$timestamp'}" | |
} | |
} | |
END { | |
for (i=1;i<=length(data);i++){ | |
if (i==length(data)){ | |
str='"data[i]"' | |
} else { | |
str='"data[i]"'"," | |
} | |
print str | |
} | |
} | |
' /proc/net/dev` | |
} | |
inode(){ | |
# io innodb stats | |
echo -e `df -Ti|sed '1d;/ /!N;s/\n//;s/ \+/ /;' | awk 'BEGIN{} | |
{ | |
fstype=$2 | |
inode_total=$3 | |
inode_used=$4 | |
inode_free=$5 | |
mount=$7 | |
if (length(inode_total)==0){ | |
inode_total=0 | |
} | |
if (length(inode_used)==0){ | |
inode_used=0 | |
} | |
if (length(inode_free)==0){ | |
inode_free=0 | |
} else if (inode_free ~ "%"){ | |
inode_free=0 | |
} | |
t_metric="df.inodes.total/fstype="fstype",mount="mount | |
f_metric="df.inodes.free/fstype="fstype",mount="mount | |
u_metric="df.inodes.used/fstype="fstype",mount="mount | |
print "'{\'endpoint\':\'$endpoint\',\'metric\':\''"t_metric"'\',\'value\':'"inode_total"',\'step\':$interval,\'counterType\':\'GAUGE\',\'tags\':\'\',\'timestamp\':$timestamp'},""'{\'endpoint\':\'$endpoint\',\'metric\':\''"f_metric"'\',\'value\':'"inode_free"',\'step\':$interval,\'counterType\':\'GAUGE\',\'tags\':\'\',\'timestamp\':$timestamp'},""'{\'endpoint\':\'$endpoint\',\'metric\':\''"u_metric"'\',\'value\':'"inode_used"',\'step\':$interval,\'counterType\':\'GAUGE\',\'tags\':\'\',\'timestamp\':$timestamp'}," | |
} | |
'` | |
} | |
if [ ! -e cpu ] || [ ! -e disk ];then | |
echo $(cpu) > cpu | |
disk_info_save > disk | |
exit 0 | |
fi | |
echo "[$(alive),$(load),$(mem),$(network),$(diskio)$(inode)$(diskbytes)$(cpuinfo)]" > data | |
sed -i "s@'@\"@g" data | |
$curl -X POST http://$transfer:6060/api/push --data-binary @data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
使用方式