Commit 85054141 authored by 李贺's avatar 李贺

初始化项目版本

parents
Pipeline #264 canceled with stages
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from subprocess import Popen, PIPE
from tempfile import TemporaryFile
import json
import os
import time
data = []
def get_all_mountpoint():
raw_data = Popen(['df', '-P'], stdout=PIPE, stderr=PIPE).communicate()[0].splitlines()
mountpoints = []
for line in raw_data:
if line:
element = line.split()[5]
if element.startswith('/'):
mountpoints.append(element)
return mountpoints
for path in get_all_mountpoint():
rstr = '@8qJnD&Y'
value = 0
try:
fd = TemporaryFile(dir=path)
fd.write(rstr)
fd.flush()
fd.seek(0)
content = fd.readline()
fd.close()
if rstr != content:
value = 1
except OSError, IOError:
value = 1
record = {}
record['metric'] = 'sys.disk.rw'
record['endpoint'] = os.uname()[1]
record['timestamp'] = int(time.time())
record['step'] = 60
record['value'] = value
record['counterType'] = 'GAUGE'
record['tags'] = 'mount=%s' % path
data.append(record)
if data:
print json.dumps(data)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import os
import time
data = []
queue_name = ('active', 'deferred', 'maildrop', 'incoming', 'corrupt', 'hold')
spool_dir = '/var/spool/postfix/'
def fetch_queue_length(queue):
length = 0
path = spool_dir + queue
for root, _, files in os.walk(path):
length += len(files)
return length
def create_record(queue, value):
record = {}
record['metric'] = 'sys.mail.queue.%s' % queue
record['endpoint'] = os.uname()[1]
record['timestamp'] = int(time.time())
record['step'] = 60
record['value'] = int(value)
record['counterType'] = 'GAUGE'
record['tags'] = ''
data.append(record)
for queue in queue_name:
create_record(queue, fetch_queue_length(queue))
if data:
print json.dumps(data)
#!/usr/bin/python
# -*- coding:utf-8 -*-
import re
import os
import time
import requests
import json
from collections import defaultdict
from netaddr import IPAddress
def NetTraffic():
'''
Inter-| Receive | Transmit
face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed
lo:1575908630 17026737 0 0 0 0 0 0 1575908630 17026737 0 0 0 0 0 0
em1:11349180581701 15272341772 0 281 0 30780 0 6989404564 1048307243792 4560099108 0 0 0 0 0 0
em2:469087486302 2564818739 0 0 0 0 0 526281272 97236008717 565164959 0 0 0 0 0 0
'''
net_reg = re.compile(":")
flow1 = open('/proc/net/dev')
lines = flow1.readlines()
flow1.close()
netIfs = defaultdict(dict)
for i in lines:
if net_reg.search(i) and not (re.search('lo', i) or re.search('bond', i)):
inter = i.strip().split(":")[0]
res = ' '.join(re.split(' +|\n+', i.strip().split(':')[1])).strip().split(' ')
netIfs[inter]['InBytes'] = res[0]
netIfs[inter]['InPackages'] = res[1]
netIfs[inter]['InErrors'] = res[2]
netIfs[inter]['InDropped'] = res[3]
netIfs[inter]['InFifoErrs'] = res[4]
netIfs[inter]['InFrameErrs'] = res[5]
netIfs[inter]['InCompressed'] = res[6]
netIfs[inter]['InMulticast'] = res[7]
netIfs[inter]['OutBytes'] = res[8]
netIfs[inter]['OutPackages'] = res[9]
netIfs[inter]['OutErrors'] = res[10]
netIfs[inter]['OutDropped'] = res[11]
netIfs[inter]['OutFifoErrs'] = res[12]
netIfs[inter]['OutFrameErrs'] = res[13]
netIfs[inter]['OutCompressed'] = res[14]
netIfs[inter]['OutMulticast'] = res[15]
return netIfs
def SYSNetWorks(ifcfg='/etc/sysconfig/network-scripts/ifcfg-%s', ifaces=None):
'''
DEVICE=eth3
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTO=no
ARPCHECK=no
USERCTL=no
IPV6INIT=no
MASTER=bond0
SLAVE=yes
返回wan/lan网口名称 bonding后的口不计算为wan口流量
:return:
'''
sys_config = defaultdict(dict)
for iface in ifaces:
with open(ifcfg % iface) as f:
for content in f.readlines():
try:
ifKey, ifVal = content.strip().split("=")
ifKey = ifKey.lower()
sys_config[iface][ifKey] = ifVal
except Exception as e:
continue
return sys_config
def NetIfs(netIfs=None, ifaces=None):
'''
公网和内网流量汇聚
:return:
'''
bonding = False
wan_nic = list()
lan_nic = list()
for iface in ifaces:
device = netIfs.get(iface).get('device')
master = netIfs.get(iface).get('master')
ipaddr = netIfs.get(iface).get('ipaddr')
if ipaddr:
private = IPAddress(ipaddr).is_private()
if private:
lan_nic.append(device)
else:
wan_nic.append(device)
if master:
wan_nic.append(device)
return (list(set(wan_nic)), lan_nic)
def get_hostname():
res_command = os.popen('hostname').read().strip()
return res_command if res_command else 'unknown'
def get_send_json(metric=None):
playload_lst = list()
for tag in metric.keys():
for k, v in metric[tag].items():
playload = {
"endpoint": get_hostname(),
"metric": k,
"timestamp": int(time.time()),
"step": 60,
"value": v,
"counterType": "COUNTER",
"tags": "iface=%s" % tag
}
playload_lst.append(playload)
return playload_lst
def Ifstat():
netIfs = NetTraffic()
sysNetWorks = SYSNetWorks(ifaces=netIfs.keys())
wan_face, lan_face = NetIfs(sysNetWorks, ifaces=netIfs.keys())
metric = defaultdict(dict)
metric['wan']['net.if.in.bytes'] = 0
metric['wan']['net.if.in.errors'] = 0
metric['wan']['net.if.out.bytes'] = 0
metric['wan']['net.if.out.errors'] = 0
metric['lan']['net.if.in.bytes'] = 0
metric['lan']['net.if.in.errors'] = 0
metric['lan']['net.if.out.bytes'] = 0
metric['lan']['net.if.out.errors'] = 0
for face in wan_face:
metric['wan']['net.if.in.bytes'] += int(netIfs.get(face).get('InBytes')) * 8
metric['wan']['net.if.in.errors'] += int(netIfs.get(face).get('InErrors')) * 8
metric['wan']['net.if.out.bytes'] += int(netIfs.get(face).get('OutBytes')) * 8
metric['wan']['net.if.out.errors'] += int(netIfs.get(face).get('OutErrors')) * 8
for face in lan_face:
metric['lan']['net.if.in.bytes'] += int(netIfs.get(face).get('InBytes')) * 8
metric['lan']['net.if.in.errors'] += int(netIfs.get(face).get('InErrors')) * 8
metric['lan']['net.if.out.bytes'] += int(netIfs.get(face).get('OutBytes')) * 8
metric['lan']['net.if.out.errors'] += int(netIfs.get(face).get('OutErrors')) * 8
playload = get_send_json(metric)
r = requests.post("http://127.0.0.1:1988/v1/push", data=json.dumps(playload))
if __name__ == '__main__':
Ifstat()
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from subprocess import Popen, PIPE
import json
import os
import time
def fetch_ntp_state():
# fail 获取状态: 0 获取成功, 1 获取失败
# offset ntp同步的偏移量, 使用绝对值表示
# timeout 超时状态: 0 成功, 1 超时
offset, fail, timeout = 0, 1, 0
try:
raw_data = Popen(['ntpq', '-pn'], stdout=PIPE, stderr=PIPE).communicate()[0]
for line in raw_data.splitlines():
if line.startswith('*'):
l = line.split()
when, poll, offset = l[4], l[5], l[8]
offset = abs(float(offset))
timeout, fail = check_status(when, poll)
except OSError:
pass
create_record('sys.ntp.fail', fail)
create_record('sys.ntp.timeout', timeout)
create_record('sys.ntp.offset', offset)
# 判断上次同步状态, return (timeout, fail)
def check_status(when, poll):
timeout, fail = 0, 1
try:
if int(poll) - int(when) >= 0:
timeout, fail = 0, 0
else:
timeout, fail = 1, 0
except:
pass
return timeout, fail
def create_record(metric, value):
record = {}
record['Metric'] = metric
record['Endpoint'] = os.uname()[1]
record['Timestamp'] = int(time.time())
record['Step'] = 600
record['Value'] = value
record['CounterType'] = 'GAUGE'
record['TAGS'] = ''
data.append(record)
if __name__ == '__main__':
retry = 3
retry_interval = 3
for i in range(0, retry):
data = []
fetch_ntp_state()
if data[0]['Value'] == 0 and data[1]['Value'] == 0 or retry == i+1:
break
time.sleep(retry_interval)
print json.dumps(data)
#coding:utf-8
import psutil
import time,json
endpoint="10.17.116.3"
class process():
def getProcessCpu(self,res):
result=[]
for i in res:
p = psutil.Process(i["pid"])
tmp_memory_percent={
"Endpoint":endpoint,
"TAGS":"",
"Timestamp":int(time.time()),
"Metric": "sys.process.memory.percent",
"CounterType":"GAUGE",
"pid":i["pid"],
"Value":p.memory_percent(),
"name":i["name"],
"cmd": ' '.join(p.cmdline()),
"Step": 90
}
tmp_cpu_percent={
"Endpoint":endpoint,
"TAGS":"",
"Timestamp":int(time.time()),
"Metric": "sys.process.cpu.percent",
"CounterType":"GAUGE",
"pid":i["pid"],
"Value":p.cpu_percent(interval=1),
"name":i["name"],
"cmd": ' '.join(p.cmdline()),
"Step": 90
}
result.append(tmp_memory_percent)
result.append(tmp_cpu_percent)
return result
if __name__ == "__main__":
result=[]
m=process()
for i in psutil.process_iter():
ps = i.as_dict(attrs=["pid","name"])
p = psutil.Process(ps["pid"])
ps["memory"]=p.memory_percent()
result.append(ps)
result=sorted(result,key=lambda x: x["memory"],reverse=True)[0:60]
res=m.getProcessCpu(result)
print json.dumps(res)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment