163 lines
5.9 KiB
Python
163 lines
5.9 KiB
Python
import os
|
|
import re
|
|
from time import sleep
|
|
from datetime import datetime
|
|
import time
|
|
import requests
|
|
import json
|
|
# import schedule
|
|
|
|
bot_token_gr = "1072981445:AAGR5pcUbHI7acIvcRQWRTAmCIMPG4dacn4"
|
|
bot_chatID_gr = "-329625622"
|
|
bot_token_SOnTV = "1915371572:AAElb2Ea3ZzAoZvOM2B8DrjmtCtANtMppt0"
|
|
bot_chatID_SOnTV = "763038225"
|
|
name_server = 'Server Richy'
|
|
service = 'docker'
|
|
now = datetime.now()
|
|
current_time = now.strftime("%H:%M:%S")
|
|
memUsed = 0
|
|
def check_stt_service():
|
|
status = os.popen('sudo service' + ' ' + service + ' ' + 'status | grep running | wc -l')
|
|
stt = status.read()
|
|
return stt
|
|
|
|
def monitor_docker():
|
|
lines = readfile()
|
|
status_service = check_stt_service().split("\n")[0]
|
|
print('Service docker status = ' + status_service)
|
|
if status_service == '1':
|
|
for line in lines:
|
|
print(line.strip())
|
|
l = line.strip().split('_')
|
|
lts = ''.join([str(e) for e in l])
|
|
# o = os.popen('docker inspect' + ' ' + line.strip())
|
|
try:
|
|
o = os.popen('sudo docker inspect' + ' ' + line.strip())
|
|
data = json.loads(o.read())
|
|
# print(data)
|
|
try:
|
|
stt = data[0]['State']['Health']['Status']
|
|
# print(stt)
|
|
if stt == 'healthy':
|
|
print('docker inspect service ' + line.strip() + ' = ' + stt)
|
|
pass
|
|
else:
|
|
print('docker inspect service ' + line.strip() + ' = ' + stt)
|
|
telegram_bot_sendtext(name_server + ' ' + ' container' + ' ' + lts + ' ' + stt)
|
|
except:
|
|
stt = data[0]['State']['Status']
|
|
# print(stt)
|
|
if stt == 'running':
|
|
print(stt)
|
|
pass
|
|
else:
|
|
print('docker inspect service ' + line.strip() + ' = ' + stt)
|
|
# print('---------------------')
|
|
telegram_bot_sendtext(name_server + ' container' + ' ' + lts + ' ' + stt)
|
|
except:
|
|
print('Do not inspect service ' + line.strip())
|
|
telegram_bot_sendtext(name_server + ' ' + ' container' + ' ' + lts + ' ' + ' not inspect')
|
|
else:
|
|
print(status_service)
|
|
telegram_bot_sendtext(name_server + ' service ' + service + ' status dead. Turn on now!!!')
|
|
|
|
|
|
def get_memory():
|
|
t = time.localtime()
|
|
dtime = (str(t.tm_hour) + ':' + str(t.tm_min) + ':' + str(t.tm_sec) + ' ' + str(t.tm_mday) + '/' + str(t.tm_mon) + '/' + str(t.tm_year))
|
|
hournow = t.tm_hour
|
|
cmd = 'cat /proc/meminfo'
|
|
i = os.popen(cmd).read()
|
|
mem = i.split()
|
|
memTotal = float(mem[1])
|
|
memAvailable = float(mem[7])
|
|
memUsed = round((memTotal - memAvailable) * 100 / memTotal)
|
|
swapTotal = float(mem[43])
|
|
swapFree = float(mem[46])
|
|
swapUsed = round((swapTotal-swapFree) * 100 /swapTotal)
|
|
if memUsed >= 80:
|
|
print(str(dtime) + ' ' + 'Memory used: ', memUsed, '%')
|
|
# telegram_bot_sendtext(name_server + ' ' + ' memory used:' + ' ' + str(memUsed))
|
|
if hournow in range(1,5):
|
|
print(hournow)
|
|
print("Send message to telegram!!!")
|
|
telegram_bot_sendtext(name_server + ' ' + ' % Memory used:' + ' ' + str(memUsed) + ' . reboot server')
|
|
# os.system('sudo reboot')
|
|
else:
|
|
print("Send message to telegram!!!")
|
|
telegram_bot_sendtext(name_server + ' ' + ' % Memory used:' + ' ' + str(memUsed))
|
|
else:
|
|
print(str(dtime) + ' ' + 'Memory used: ', memUsed, '%')
|
|
if swapUsed >= 80:
|
|
print(str(dtime) + ' ' + 'Swap used: ', swapUsed, '%')
|
|
telegram_bot_sendtext(name_server + ' ' + ' % Swap used:' + ' ' + str(swapUsed))
|
|
else:
|
|
print(str(dtime) + ' ' + 'Swap used: ', swapUsed, '%')
|
|
return memUsed, swapUsed
|
|
|
|
def get_cpu():
|
|
t = time.localtime()
|
|
dtime = (str(t.tm_hour) + ':' + str(t.tm_min) + ':' + str(t.tm_sec) + ' ' + str(t.tm_mday) + '/' + str(t.tm_mon) + '/' + str(t.tm_year))
|
|
cmd = 'grep "cpu " /proc/stat'
|
|
output = os.popen(cmd)
|
|
cpu = output.read().strip().split(' ')
|
|
user = float(cpu[2])
|
|
system = float(cpu[4])
|
|
idle = float(cpu[5])
|
|
cpu_used = round((user + system)*100 / (user + system + idle), 1)
|
|
if cpu_used >= 80:
|
|
print(str(dtime) + ' ' + 'CPU used: ', cpu_used, '%')
|
|
telegram_bot_sendtext(name_server + ' ' + 'CPU used:' + ' ' + str(cpu_used))
|
|
else:
|
|
print(str(dtime) + ' ' + 'CPU used: ', cpu_used, '%')
|
|
pass
|
|
|
|
def get_free_space():
|
|
cmd = 'df -H /dev/sda2 --output=pcent'
|
|
output = os.popen(cmd).read().split("\n")
|
|
d_used = output[1].split("%")
|
|
d = d_used[0].split(" ")
|
|
if int(d[1]) >= 95:
|
|
disk = d[1]
|
|
print("Disk_used = " + d[1] + "%")
|
|
telegram_bot_sendtext(name_server + ' ' + '% DISK used =' + ' ' + disk)
|
|
else:
|
|
print("Disk_used = " + d[1] + "%")
|
|
pass
|
|
|
|
def telegram_bot_sendtext(bot_message):
|
|
bot_token = bot_token_gr
|
|
bot_chatID = bot_chatID_gr
|
|
send_text = 'https://api.telegram.org/bot' + bot_token + '/sendMessage?chat_id=' + bot_chatID + '&parse_mode=Markdown&text=' + bot_message
|
|
|
|
response = requests.get(send_text)
|
|
return response.json()
|
|
|
|
def readfile():
|
|
try:
|
|
file = open('hosts.txt', 'r')
|
|
Lines = file.readlines()
|
|
return Lines
|
|
finally:
|
|
file.close()
|
|
mem = get_memory()
|
|
bot_message = (name_server + ' ' + 'memory used:' + ' ' + str(mem[0]) + ' ' + 'swapused:' + ' ' + str(mem[1]))
|
|
while(True):
|
|
now = datetime.now()
|
|
current_time = now.strftime("%H:%M")
|
|
print("current_time = " + current_time)
|
|
time_now = time.localtime()
|
|
get_free_space()
|
|
get_cpu()
|
|
get_memory()
|
|
# mem = get_memory()
|
|
|
|
bot_test = ("hello")
|
|
if current_time == '09:00':
|
|
telegram_bot_sendtext(bot_message)
|
|
monitor_docker()
|
|
# print(memUsed)
|
|
# print(swapUsed)
|
|
|
|
sleep(59)
|