Skip to content

Instantly share code, notes, and snippets.

@roelandp
Created January 15, 2018 17:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roelandp/6b03e873d206ed5c5ac862d14d4d550c to your computer and use it in GitHub Desktop.
Save roelandp/6b03e873d206ed5c5ac862d14d4d550c to your computer and use it in GitHub Desktop.
Simple Server Reporter - sharing process %, RAM %, HDD free/used space - sharing to you via Telegram Report
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import os
import requests
import psutil
servername = "IDENTIFIABLE SERVER NAME"
diskmountedon = "/"
telegram_token = "" # Create your Telegram bot at @BotFather (https://telegram.me/botfather)
telegram_id = # Get your telegram id at @MyTelegramID_bot (https://telegram.me/mytelegramid_bot)
# Telegram barebones apicall
def telegram(method, params=None):
url = "https://api.telegram.org/bot"+telegram_token+"/"
params = params
r = requests.get(url+method, params = params).json()
return r
# Telegram notifyer
def send_telegram(msg):
# Send TELEGRAM NOTIFICATION
payload = {"chat_id":telegram_id, "text":msg,"parse_mode":"HTML"}
m = telegram("sendMessage", payload)
disk = os.statvfs(diskmountedon)
totalBytes = float(disk.f_bsize*disk.f_blocks)
totalUsedSpace = float(disk.f_bsize*(disk.f_blocks-disk.f_bfree))
totalAvailSpaceNonRoot = float(disk.f_bsize*disk.f_bavail)
pctuse = totalUsedSpace/totalBytes * 100
# using psutil for disk usage...
cpu = (psutil.cpu_percent(interval=1, percpu=True))
mem = psutil.virtual_memory()
cputext = ""
i = 0
for res in cpu:
i = i+1
cputext = cputext + "Core "+str(i)+": "+str(res)+"%\n"
pctram = (mem.total - mem.available) / mem.total * 100
witnesstext = "📋 <b>"+servername+"</b> 📋\n\n<b>DiskSpace:</b>\nAvailable: %.2f GB.\nUsed: %.2f GB (%.1f%%)\n\n<b>CPU</b>\n%s\n<b>RAM</b>\nAvailable: %.2f GB\nUsed: %.2f GB (%.1f%%)" % (totalAvailSpaceNonRoot/1024/1024/1024, totalUsedSpace/1024/1024/1024, pctuse, cputext, mem.available/1024/1024/1024,(mem.total-mem.available)/1024/1024/1024, pctram)
send_telegram(witnesstext)
@bosjahat
Copy link

I'm waiting for a big event at Steemfest3. hello @roelandp and everything in SteemFest. greetings from me @ridwant from Indonesia

@bosjahat
Copy link

thank you @roelandp. but I do not understand. how can I'm speak a little with you to say Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment