Nagios – Check Ping /bin/ping Unknown status problem

Recently while I am trying to setup Nagios on Ubuntu 14.04 OS I got the error saying /bin/ping Unknown status.

After debugging a wile I got to know that, this issue is because of permissions for /bin/ping script.

To resolve this issue just run following command ( use root or sudo):

$ chmod u+s /bin/ping

After running above command /bin/ping script permissions look like below:

$ ls -l /bin/ping

-rwsr-xr-x 1 root root 44168 Mar 15  2014 /bin/ping*

After a while, nagios should be able to ping your servers without any issue.

Ubuntu 14.04 – ping icmp open socket operation not permitted

Recently when I am trying to do ping got following exception:

$ ping host.example.com

Output:

ping: icmp open socket: Operation not permitted

Reason for the issue:

On Linux (and other flavours) you have to be root to open up a socket. The SUID bit must be set in the ping binary to allow it to open sockets. This issue is common on jailing users as most disto’s ping binary will have this set by default.

Set the SUID bit:

chmod u+s bin/ping

Now check SUID bit:

ls -la bin/ping

Output:
-rwsr-xr-x 1 root root 41704 2011-04-06 15:13 bin/ping
Now you can try ping command, it should work without any issue.

Python – convert dictionary to json

 

To convert any dictionary to json use json.dumps(YourDictionary).

Below is an example to convert a dictionary to json:

import json

def toJson():
    d = {'a' : 'Apple', 'b' : 2}
    return json.dumps(d)

print toJson()

Output:

{“a”: “Apple”, “b”: 2}

lighttpd – allow/block ip range

 

To allow some or multiple ip rages use below code:

$HTTP["remoteip"] =~ "192\.168\.1\.*|10\.2\.20\.*" {
    server.document-root = "/var/www"
}

Above code will allow only 192.18.1.* and 10.2.20.* ip rages and blocks all other ip’s.

To block some or multiple ip ragnes use below code

$HTTP["remoteip"] =~ "192\.168\.1\.*|10\.2\.20\.*" {
    url.access-deny = ( "" )
}

Above code will block 192.168.1.* and 10.2.20.* ip rages and allow all other ip’s.

awk – print last field

 

NF is built in variable in awk which stores Number of fields.

To get last field with awk use following example:

$ echo a b c | awk '{print $NF}'

Output:
c

By default awk will take any white space character(space, tab, …) to split.

To get last but one field with awk:

$ echo a b c | awk '{print $(NF-1)}'

Output:
b

To print only number of fields with awk:

$ echo a b c | awk '{print NF}'

Output:
3

Configuring mod_dumpio with apache2 in Ubuntu

 mod_dumpio allows for the logging of all input received by Apache and/or all output sent by Apache to be logged (dumped) to the error.log file.

The data logging is done right after SSL decoding (for input) and right before SSL encoding (for output). As can be expected, this can produce extreme volumes of data, and should only be used when debugging problems.

To configure mod_dumpio you need to enable dump_io.

$ sudo a2enmod dump_io

Next add below mentioned 3 lines in /etc/apache2/apache2.conf

#To capture http request
DumpIOInput On

#To capture http response
DumpIOOutput On 

#To capture everything
DumpIOLogLevel debug

Now change LogLevel in your /etc/apache2/sites-enabled/000-default or /etc/apache2/sites-available/default file to debug.

LogLevel debug

After doing all above changes restart apache2.

$ service apache2 restart

To view logs you need to check /var/log/apache2/error.log

$ tail -f /var/log/apache2/error.log

To test make a dummy request as shown below and at the same time view error.log file where you can lot of lines related to this request.

$ curl http://127.0.0.1/ -d group=user -d sort=name

Since dump_io will create log of disk io, enabling it in production servers is not a good idea. It will slow down your production server.

How to clear swap space – Ubuntu/Linux

 

To clear swap space in Ubuntu/Linux we need to use swapoff command as shown below:

$ swapoff -a

Above command will turn of swap completely.

Before running swapoff command you need to have enough free RAM available, since swapoff command will move all swap data to RAM.

If you wont have enough free RAM this command may make your system unstable.

Again to turn on swap you need to use swapon command ash shown below:

$ swapon -a

You need to have root/sudo permissions to run above commands.

Number of open files or file descriptors associated with a process

Since in *nix every thing is a file, When you try to find open files or file descriptor associated with a process you will see Actual file log, Unix Sockets, Library files, Network Sockets,  /dev files and others.

lsof is a command to list all open files.

To list all open files or file descriptors by a process:

$ lsof -p PID

or

$ lsof -a -p PID

or

$ ls /proc/PID/fd

where PID is a process ID of any process that is running.

To count all open files by a process:

$ lsof -p PID | wc -l

or

$ ls /proc/PID/fd | wc -l

To list all open files:

$ lsof 

To count all open files:

$ lsof | wc -l

Python – remove newline from readlines

 

readlines() is a function in python used to read text file and convert it into list where each line in at file is a element in list with new line(\n) at the end of each element.

Example:

>>> filelist = open("file.txt").readlines()
['a\n', 'b\n', 'c\n', 'd\n']

If you want to remove new line(\n) at the end of all elements in list use read() and splitlines() functions as shown in below example.

read().splitlines() will do same funcationality as readlines() & only difference is read().splitlines() won’t have have new line(‘\n’) at the end of all elements in list.

Example:

>>>open("file.txt").read().splitlines()
['a', 'b', 'c', 'd']

Python – check if string starts with number

With the help of isdigit() function we can check if a string start with number or not.

Lets check with following examples:

>>> input = '1viewsby'
>>> input[0].isdigit()
True

Above example is returned true since input string is started with with number.

>>> input = 'viewsby'
>>> input[0].isdigit()
False

Since input is is not started with number above example returned false.