Sending SMS from server

Several days ago I looked into details of how my selectel server spended money and saw that it spends about 20 roubles per day (about $0.8) and has 100% CPU load for about 2 last days. I connected to it and saw that some 'sh' proccess is hang. I just killed it.

So as to react in time to such cases I decided to set up an SMS notification.

I registered on SMSC.RU. One SMS costs 0.50 roubles (about $0.02).

I wrote small script that is watching average load and will notify if it becomes too high.

from urllib import urlopen, urlencode
import re

def send_sms(load):
    data = {
        "login": "<login>",
        "psw": "<password>",
        "phones": "<phone-number>",
        "mes": (u"Server is overloaded %.2f %.2f %.2f" % tuple(loadavg))
    }

    urlopen("http://smsc.ru/sys/send.php", urlencode(data))

f = open("/proc/loadavg", "r")
line = f.readline()
f.close()
m = re.search("^(\d+[.]\d+) (\d+[.]\d+) (\d+[.]\d+)", line)
loadavg = [float(x) for x in m.groups()]
if loadavg[2] > 0.7:
    send_sms(loadavg)
blog comments powered by Disqus