Skip to content

Commit f8586cd

Browse files
author
tommy3001
committed
Psutil system resources library to scenarios/admin added
1 parent 23dc518 commit f8586cd

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

docs/scenarios/admin.rst

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,60 @@ State files can be written using YAML, the Jinja2 template system or pure python
120120

121121
`Salt Documentation <http://docs.saltstack.org/en/latest/index.html>`_
122122

123+
124+
Psutil
125+
------
126+
`Psutil <https://code.google.com/p/psutil/>`_ is an interface to different system information (e.g. CPU, memory, disks, network, users and processes).
127+
128+
Here is an example to be aware of some server overload. In case of some failed test (net, CPU) it send an email.
129+
130+
.. code-block:: python
131+
132+
# Functions to get system values:
133+
from psutil import cpu_percent, net_io_counters
134+
# Functions to take a break:
135+
from time import sleep
136+
# Package for email services:
137+
import smtplib
138+
import string
139+
MAX_NET_USAGE = 400000
140+
MAX_ATTACKS = 4
141+
attack = 0
142+
counter = 0
143+
while attack <= MAX_ATTACKS:
144+
sleep(4)
145+
counter = counter + 1
146+
# Check the cpu usage
147+
if cpu_percent(interval = 1) > 70:
148+
attack = attack + 1
149+
# Check the net usage
150+
neti1 = net_io_counters()[1]
151+
neto1 = net_io_counters()[0]
152+
sleep(1)
153+
neti2 = net_io_counters()[1]
154+
neto2 = net_io_counters()[0]
155+
# Calculate the bytes per second
156+
net = ((neti2+neto2) - (neti1+neto1))/2
157+
if net > MAX_NET_USAGE:
158+
attack = attack + 1
159+
if counter > 25:
160+
attack = 0
161+
counter = 0
162+
# Write a very important email if attack is higher then 4
163+
TO = "you@your_email.com"
164+
FROM = "webmaster@your_domain.com"
165+
SUBJECT = "Your domain is out of system resources!"
166+
text = "Go and fix your server!"
167+
BODY = string.join(("From: %s" %FROM,"To: %s" %TO,"Subject: %s" %SUBJECT, "",text), "\r\n")
168+
server = smtplib.SMTP('127.0.0.1')
169+
server.sendmail(FROM, [TO], BODY)
170+
server.quit()
171+
172+
173+
A full terminal application like a widely extended top which is based on psutil and with the ability of a client-server
174+
monitoring is `glance <https://github.com/nicolargo/glances/>`_.
175+
176+
123177
Chef
124178
----
125179

0 commit comments

Comments
 (0)