mirror of
https://github.com/kennethreitz/python-guide.git
synced 2026-06-05 23:00:18 +00:00
Psutil system resources library to scenarios/admin added
This commit is contained in:
@@ -120,6 +120,60 @@ State files can be written using YAML, the Jinja2 template system or pure python
|
||||
|
||||
`Salt Documentation <http://docs.saltstack.org/en/latest/index.html>`_
|
||||
|
||||
|
||||
Psutil
|
||||
------
|
||||
`Psutil <https://code.google.com/p/psutil/>`_ is an interface to different system information (e.g. CPU, memory, disks, network, users and processes).
|
||||
|
||||
Here is an example to be aware of some server overload. In case of some failed test (net, CPU) it send an email.
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
# Functions to get system values:
|
||||
from psutil import cpu_percent, net_io_counters
|
||||
# Functions to take a break:
|
||||
from time import sleep
|
||||
# Package for email services:
|
||||
import smtplib
|
||||
import string
|
||||
MAX_NET_USAGE = 400000
|
||||
MAX_ATTACKS = 4
|
||||
attack = 0
|
||||
counter = 0
|
||||
while attack <= MAX_ATTACKS:
|
||||
sleep(4)
|
||||
counter = counter + 1
|
||||
# Check the cpu usage
|
||||
if cpu_percent(interval = 1) > 70:
|
||||
attack = attack + 1
|
||||
# Check the net usage
|
||||
neti1 = net_io_counters()[1]
|
||||
neto1 = net_io_counters()[0]
|
||||
sleep(1)
|
||||
neti2 = net_io_counters()[1]
|
||||
neto2 = net_io_counters()[0]
|
||||
# Calculate the bytes per second
|
||||
net = ((neti2+neto2) - (neti1+neto1))/2
|
||||
if net > MAX_NET_USAGE:
|
||||
attack = attack + 1
|
||||
if counter > 25:
|
||||
attack = 0
|
||||
counter = 0
|
||||
# Write a very important email if attack is higher then 4
|
||||
TO = "you@your_email.com"
|
||||
FROM = "webmaster@your_domain.com"
|
||||
SUBJECT = "Your domain is out of system resources!"
|
||||
text = "Go and fix your server!"
|
||||
BODY = string.join(("From: %s" %FROM,"To: %s" %TO,"Subject: %s" %SUBJECT, "",text), "\r\n")
|
||||
server = smtplib.SMTP('127.0.0.1')
|
||||
server.sendmail(FROM, [TO], BODY)
|
||||
server.quit()
|
||||
|
||||
|
||||
A full terminal application like a widely extended top which is based on psutil and with the ability of a client-server
|
||||
monitoring is `glance <https://github.com/nicolargo/glances/>`_.
|
||||
|
||||
|
||||
Chef
|
||||
----
|
||||
|
||||
|
||||
Reference in New Issue
Block a user