Follow me!

LinkedIn Twitter

Who's Online

We have 151 guests online

Python News



  

Comparing active values from sysctl.conf using Python
Programming HowTo's - Python HowTo's
Written by Allen Sanabria   
Wednesday, 14 May 2008 15:01

I was told the other day to write a script that will compare a set of predefined values  to active values from "sysctl -a"
So here it is.... Some people might ask why did I not do this in shell or even perl??? The answer is simple I love PYTHON!!! :)

Example below..... Make sure to run as root 

python sysctl_verify.py
[FAIL] net.core.rmem_max = 131071 (Required value = 20971520)
[FAIL] net.ipv4.tcp_mem = 374304 (Required value = 32768 32768 32768)
[FAIL] net.ipv4.tcp_rmem = 4096 (Required value = 4096 87380 8388608)
[FAIL] net.ipv4.tcp_retries2 = 15 (Required value = 5)
[FAIL] net.ipv4.tcp_wmem = 4096 (Required value = 4096 16384 8388608)
[FAIL] net.core.wmem_max = 131071 (Required value = 20971520)
[FAIL] net.core.netdev_max_backlog = 1000 (Required value = 3000)
[FAIL] net.ipv4.route.flush = Does not Exist!! (Required value = 1)
[FAIL] Current RX value = 128 (Required RX value = 16384)



#!/usr/bin/env python
#This script will will compare a set of predefined values  to active values from "sysctl -a"
#Copyright (C) 2008 Allen Sanabria

#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.

#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License along
#with this program; if not, write to the Free Software Foundation, Inc.,
#51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
import os, sys, re, string
#Created by Allen Sanabria
#Verify these parameters are whats running in sysctl
# net.core.rmem_max = 20971520
# net.core.wmem_max = 20971520
# net.ipv4.tcp_mem = 32768 32768 32768
# net.ipv4.tcp_rmem = 4096 87380 8388608
# net.ipv4.tcp_wmem = 4096 16384 8388608
# net.ipv4.tcp_retries2 = 5
# net.core.netdev_max_backlog = 3000
# net.ipv4.route.flush = 1

uid = os.getuid()
root = 0
login = os.getlogin()
eth = []
max_rx = {}
cur_rx = {}

if uid == root:
sysctl_out = {'net.core.rmem_max' : '20971520',
'net.core.wmem_max' : '20971520',
'net.ipv4.tcp_mem' : '32768 32768 32768',
'net.ipv4.tcp_rmem' : '4096 87380 8388608',
'net.ipv4.tcp_wmem' : '4096 16384 8388608',
'net.ipv4.tcp_retries2' : '5',
'net.core.netdev_max_backlog' : '3000',
'net.ipv4.route.flush' : '1'}


for key in sysctl_out.keys():
sysctl = 'sysctl -a | grep %s' % (key)
sysctl_exec = os.popen(sysctl).readline()
sysctl_exec = re.sub('\n', '', sysctl_exec)
sysctl_exec = re.sub('\s+', ' ', sysctl_exec)
sysctl_match = re.search(key+' = '+sysctl_out[key], sysctl_exec)
sysctl_line = re.search((key)+' = (\d+)', sysctl_exec)
sysctl_first = re.search((key), sysctl_exec)
if sysctl_match:
print "[PASS] "+key+ " = " +sysctl_out[key]
elif not sysctl_first:
print "[FAIL] " +key+ " = Does not Exist!! (Required value = " +sysctl_out[key]+ ")"
else:
print "[FAIL] " +key+ " = " +sysctl_line.group(1)+ " (Required value = " +sysctl_out[key]+ ")"
else:
print "Only root can run this, and you are %s with a id of %s" % (login, uid)



Add this page to your favorite Social Bookmarking websites
Reddit! Del.icio.us! Mixx! Free and Open Source Software News Google! Live! Facebook! StumbleUpon! Yahoo! Free Joomla PHP extensions, software, information and tutorials.
Comments
Search RSS
dynasty   |SAdministrator |2008-05-16 16:03:24
I just updated this script to print out much neater...
Only registered users can write comments!

3.22 Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."

Last Updated on Saturday, 19 July 2008 14:02