/pySar

Python Library for accessing sysstat

Primary LanguagePythonGNU General Public License v2.0GPL-2.0

Description

pySar is a Python library for reading sysstat output

Need a working sysstat setup

[me@webapp01 pySar]$ sar | head
Linux 3.3.4-5.fc17.x86_64 (webapp01)    09/04/2012      _x86_64_        (4 CPU)

12:00:02 AM     CPU     %user     %nice   %system   %iowait    %steal     %idle
12:10:01 AM     all      0.06      0.00      0.89      0.57      0.01     98.48
12:20:02 AM     all      0.07      0.00      1.37      0.67      0.01     97.88
12:30:01 AM     all      0.07      0.00      1.66      0.57      0.01     97.69
12:40:02 AM     all      0.05      0.00      0.68      0.69      0.01     98.57
12:50:01 AM     all      0.05      0.00      0.59      0.24      0.00     99.12
01:00:02 AM     all      0.07      0.00      0.99      0.75      0.01     98.17
01:10:01 AM     all      0.06      0.00      0.56      0.18      0.00     99.20

Using the “pysar” command

[me@webapp01 pySar]$ pysar --help
usage: pysar [-h] [--pretty]
             sysstat report type to pull. ['cpu', 'load', 'memory', 'swap']

positional arguments:
  sysstat report type to pull. ['cpu', 'load', 'memory', 'swap']

optional arguments:
  -h, --help            show this help message and exit
  --pretty              Print json output in human readable form.

[me@webapp01 pySar]$ pysar cpu --pretty | head -n 11
[
    {
        "%steal": "0.01", 
        "%user": "0.08", 
        "timestamp": "00:10:01", 
        "%idle": "97.49", 
        "%iowait": "0.73", 
        "CPU": "all", 
        "%nice": "0.00", 
        "%system": "1.70"
    },

Getting CPU usage with python

>>> from pySar.lib import sar
>>> results = sar(sarbin='/bin/sar')

>>> len(results)
110
>>> results[0].keys()
['%steal', '%user', 'timestamp', '%idle', '%iowait', 'CPU', '%nice', '%system']
>>> results[0]['timestamp']
'12:10:01'
>>> results[0]['%idle']
'98.48'
>>> results[0]['%iowait']
'0.57'

Getting Memory usage with python

>>> results = sar(sarbin='/bin/sar', saroptions='-r')

>>> results[0].keys()
['kbactive', '%commit', 'timestamp', '%memused', 'kbmemfree', 'kbcommit',
'kbbuffers', 'kbmemused', 'kbinact', 'kbcached']
>>> results[0]['timestamp']
'12:10:01'
>>> results[0]['kbmemfree']
'6452'

Access historic logs with the “sarfile” option with python

>>> results = sar(sarbin='/bin/sar', saroptions='-q', sarfile='/var/log/sa/sa03')

>>> results[0].keys()
['plist-sz', 'ldavg-15', 'timestamp', 'runq-sz', 'ldavg-1', 'ldavg-5', 'blocked']
>>> results[0]['ldavg-5']
'0.01'