Saturday, April 20, 2013

syslog, rsyslog - Centralized Logging


Basically logs generated by server daemons such as Apache or applications, are streams and not files, though the application/daemon have a configuration parameter for logging into a file.

Why logs are considered as streams? Because logs do not have beginning or end, they are just ongoing.

How Linux handles logs(streams)?

In Linux, stdout and stderr, are two default output streams, which are automatically available to all programs. These streams can be turned into files using a redirect operator.

An application which uses stdout for logging can be made to log to a file as follows
$ application >> /var/log/applogfile

Syslog:

So when log messages from an application can be directed to go to files(using redirection operator) or user terminals or run them through other programs (with a pipe - to email, pager, or just a log file analyzer), here comes a question of how to do distributed logging or logging to a centralized place from multiple hosts. syslog protocol comes to the aid here. Now we have modern logging protocols such as Scribe and Splunk for example.

As per RFC-3164:
In its most simplistic terms, the syslog protocol provides a transport to allow a machine to send event notification messages across IP networks to event message collectors - also known as syslog servers.  Since each process, application and operating system was written somewhat independently, there is little uniformity to the content of syslog messages.  For this reason, no assumption is made upon the formatting or contents of the messages.  The protocol is simply designed to transport these event messages.  In all cases, there is one device that originates the message.  The syslog process on that machine may send the message to a collector.  No acknowledgement of the receipt is made.

syslog protocol helps to send logs from many components to a single location. Programs which wish to use syslog protocol for logging, should have syslog awareness implemented in them. However, a program which used stdout for logging, can use syslog without needing to implement any syslog awareness into the program, by piping to the standard logger command.

$ myapplication | logger

However we can split the log stream to a local file as well as to syslog as follows

$  myapplication | tee /var/log/myapplication.log | logger

When set up to use a syslog server, devices will send their log messages over the network wire to the syslog server rather than recording them in a local file or displaying them.

Syslog is three things:

1) /dev/log, a UNIX-domain socket. Applications can connect to it and send it "messages".
2) UDP port 514, which is another service upon which applications can send messages. More importantly, this allows messages to be transferred from one host to another.
3) A program — sysklogd, syslog-ng, rsyslog, or one of several other variants of syslog — that listens on these sockets and ports, reads the messages, and decides where the messages should be sent, usually one or more files.

In my system, rsyslog program is being used and it uses a Unix domain socket

[root@dhcppc0 ~]# netstat -anp | grep rsyslog
unix  22     [ ]         DGRAM                    10559  1331/rsyslogd       /dev/log

rsyslog is an enhanced, multi-threaded syslog.

From here onwards, I shall be using rsyslog and syslog interchangeably as

What gets logged by rsyslogd daemon and where it goes is controlled by /etc/rsyslog.conf. A modern system uses rsyslog to centralize logging.

Here's how it works: A developer uses the rsyslog API function (or uses the logger program in shell scripts) to send log messages to syslogd. The information passed to rsyslogd includes the source of the log message (called a facility) and the priority of the log message.

rsyslogd then matches the facility and priority against selectors (combinations of facilities and priorities) in its configuration file. For the selector(s) that match the messages is sent to the corresponding destination(s).

Most log files go under /var/log directory. Besides the more specific log files, there is a general system log file usually called messages. Other important log files to monitor include: boot.log, dmesg (also the dmesg command), maillog, secure, wtmp (examine with the last command).

Log files contain sensitive information! You must protect these files by setting permisions carefully!

Log messages don't only have to go to files, you can direct them to user terminals, run them through other programs (with a pipe, to email, pager, or just a log file analyzer), or send them to another host running syslogd.

Following severity levels that can assigned to messages

0 - Emergency (emerg)
1 - Alerts (alert)
2 - Critical (crit)
3 - Errors (err)
4 - Warnings (warn)
5 - Notification (notice)
6 - Information (info)
7 - Debug (debug)

The messages can be categorized as follows. These things are also called as "facilities"

auth          The authorization system. Ex.: login, su, ftpd, rshd
authpriv User access messages use this
cron          Used by the cron facility
daemon Other daemon programs without a facility of their own
ftp          Used by ftp applications
kern          Kernel messages
lpr         The line printer spooling system
mail         Used by mail applications
mark         Used by syslogd to produce timestamps in log files
news         Used by news applications
security Same as auth. Should not be used anymore.
syslog messages from the syslog process itself
user         Messages generated by random user processes. Default.
uucp         UUCP messages
local0 – local7 Reserved for local use.
*         For all

In /etc/rsyslog.conf, we can specify

1)  Different severity levels can be specified for different facilities.
2)  Also, where the message goes(files, user, pipes)

The entry format is as follows

       facility.severity         log-file-name

Sample entries in /etc/rsyslog.conf file would look like as follows

1) Do NOT redirect facilities mail, authentication and cron and mail to /var/log/messages, look for the keyword none

       *.info;mail.none;authpriv.none;cron.none                /var/log/messages

2) The authpriv file has restricted access.

                  authpriv.*                                              /var/log/secure

3) Log all the mail messages in one place.

                    mail.*                                                  -/var/log/maillog

4)  Log cron stuff


                  cron.*                                                  /var/log/cron

5) To redirect all incoming messages from all facilities and with all severities to /var/log/syslog

                     *.*            -/var/log/syslog

6) To filter out messages with severity critical and save to file /var/log/critical

                       *.crit           -/var/log/critical

What's that dash in front of the filenames in /etc/rsyslog.conf file?

This is to avoid rsyslogd or syslog daemon becoming a bottleneck for the performance of the system. rsyslogd/syslogd daemon uses fsync() to flush very file write. This is done to reduce the chances of log information getting lost before being written to the disk in case of a crash. By prefixing the names of the log files with a dash/hyphen, we specify that log file writes should not be flushed to disk immediately.

How to configure rsyslog for centralized logging?


Central Log Host

1) In the central log host, first setup "rsyslogd" to accept remote messages. This shall be done by uncommenting the following lines under "Modules" section in  "/etc/rsyslog.conf" file in central log host as follows
          # Provides UDP syslog reception
          $ModLoad imudp
          $UDPServerRun 514

Opening up UDP port 514 for receiving messages from remote servers. 
UDP/TCP capability is not enabled by default for rsyslog. Hence we need to load the modules imudp(for UDP) and imtcp(for TCP) to enable UDP or TCP support.

2) Restart syslogd in central log host
           service rsyslog restart
   
   Verify if rsyslog is listening on UDP port 514

   [root@dhcppc0 etc]# netstat -tulnp | grep 514
   udp        0      0 0.0.0.0:514                 0.0.0.0:*                               3826/rsyslogd
   udp        0      0 :::514                      :::*                                    3826/rsyslogd

Now the central machine shall start accepting log messages from other machines on UDP port 514

Remote Host

Now the client machines need to be configured to send it's log messages to central log host listening on udp port 514. 

1) In client machines, edit the file /etc/rsyslog.conf as follows
        user.*     @central_host_ip:514

2) Then restart the syslogd service in client machines
         service rsyslog restart

3) Test the working by running the logger command in another machine

       logger -i -t <yourname> "This is test from client"  E.g. logger -i -t root  "This is a test from client"

In the central log host, check /var/log/messages for an entry This is a test from client

Get HTTP status code using curl from command line

Suppose, I want to test the availability of a website from command line, curl comes to the aid.

The following curl options are really very useful


 -s/--silent
              Silent or quiet mode. Don't show progress meter or error messages.  Makes Curl mute.


-I/--head
              (HTTP/FTP/FILE)  Fetch  the  HTTP-header only! HTTP-servers feature the command HEAD which this uses to get nothing but the header of a document. When used on a FTP or FILE file, curl displays the file size and last modification time only.

 -L/--location
              (HTTP/HTTPS)  If  the server reports that the requested page has moved to a different location (indicated with a Location: header and a 3XX response code), this option will make curl redo the request on the new place. If used  together  with  -i/--include  or  -I/--head, headers  from all requested pages will be shown.


Let us illustrate with an example


$ curl -I google.com

HTTP/1.1 301 Moved Permanently
Location: http://www.google.com/
Content-Type: text/html; charset=UTF-8
Date: Sat, 20 Apr 2013 14:14:56 GMT
Expires: Mon, 20 May 2013 14:14:56 GMT
Cache-Control: public, max-age=2592000
Server: gws
Content-Length: 219
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN

So here the HTTP status code while accessing the url, google.com, is 301, indicating a redirect. However, we do not have any info about the url to which google.com redirects. So we use the -L option of curl command as follows.

$ curl -IL google.com


HTTP/1.1 301 Moved Permanently
Location: http://www.google.com/
Content-Type: text/html; charset=UTF-8
Date: Sat, 20 Apr 2013 14:15:00 GMT
Expires: Mon, 20 May 2013 14:15:00 GMT
Cache-Control: public, max-age=2592000
Server: gws
Content-Length: 219
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN

HTTP/1.1 302 Found
Location: http://www.google.co.in/
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Set-Cookie: PREF=ID=34355ea70d42cfd3:FF=0:TM=1366467300:LM=1366467300:S=lf21z4mtM-zoJzkp; expires=Mon, 20-Apr-2015 14:15:00 GMT; path=/; domain=.google.com
Set-Cookie: NID=67=U_DZQR302SSX-7TZo3M6w0aaSBgj6l32BvBjrRO1i4Sk8Ecy6YzKDK5HBewGsgf5bB4sQI_PVRzCeeYfkUlT10X57aqV7jGBFGUx9JcAMZ0rjbIFNggpQULTqCAjil_n; expires=Sun, 20-Oct-2013 14:15:00 GMT; path=/; domain=.google.com; HttpOnly
P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."
Date: Sat, 20 Apr 2013 14:15:00 GMT
Server: gws
Content-Length: 221
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN

HTTP/1.1 200 OK
Date: Sat, 20 Apr 2013 14:15:01 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=ISO-8859-1
Set-Cookie: PREF=ID=61af3f5ebcc415a0:FF=0:TM=1366467301:LM=1366467301:S=76cA-ULyN5yq8JmR; expires=Mon, 20-Apr-2015 14:15:01 GMT; path=/; domain=.google.co.in
Set-Cookie: NID=67=uTayihoLkuwaP5vaOmQHcWVs9jJVOdKg1JucLn7Vcybi_t0_b25EZw6qAvPNYRQLkTX0Y56P5YSUWjEyd6EmcZWkPTBhxSs7jrdf0ndfpCpYcrLspEVt9SG2WCRyxzCh; expires=Sun, 20-Oct-2013 14:15:01 GMT; path=/; domain=.google.co.in; HttpOnly
P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."
Server: gws
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Transfer-Encoding: chunked


To just get the status code alone without any header information

curl -sL -w "%{http_code} %{url_effective}\\n" "URL" -o /dev/null

$ curl -sL -w "%{http_code} %{url_effective}\\n" "http://here.com" -o /dev/null
200 http://here.com

Saturday, April 13, 2013

Python : Show method(s) available for an object/module

In python, I open a file for reading as follows

f = open("/var/log/messages")

where, f is a file object.

So for performing different file operations, I would like to know the methods available for the file object f.

dir(object) comes to the aid here.

So to know the methods available for file object f

#!/usr/bin/python

f = open("/var/log/messages")
print dir(f)

The output of the above script shall be as follows

['__class__', '__delattr__', '__doc__', '__enter__', '__exit__', '__format__', '__getattribute__', '__hash__', '__init__', '__iter__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'close', 'closed', 'encoding', 'errors', 'fileno', 'flush', 'isatty', 'mode', 'name', 'newlines', 'next', 'read', 'readinto', 'readline', 'readlines', 'seek', 'softspace', 'tell', 'truncate', 'write', 'writelines', 'xreadlines']

The above script can be modified as follows, to display the method names returned as a list, one per line



#!/usr/bin/python

f = open("/var/log/messages")


for method in dir(f):
     if hasattr(f,method):
        print method

__class__
__delattr__
__doc__
__enter__
__exit__
__format__
__getattribute__
__hash__
__init__
__iter__
__new__
__reduce__
__reduce_ex__
__repr__
__setattr__
__sizeof__
__str__
__subclasshook__
close
closed
encoding
errors
fileno
flush
isatty
mode
name
newlines
next
read
readinto
readline
readlines
see
softspace
tell
truncate
write
writelines
xreadlines

To know the methods available for a module, dir() again comes to the aid


import moduleName
dir(moduleName)

Let us illustrate with an example. Suppose I want to find the methods available for module os

#!/usr/bin/python
import os

print dir(os)

The output of above script is as follows

['EX_CANTCREAT', 'EX_CONFIG', 'EX_DATAERR', 'EX_IOERR', 'EX_NOHOST', 'EX_NOINPUT', 'EX_NOPERM', 'EX_NOUSER', 'EX_OK', 'EX_OSERR', 'EX_OSFILE', 'EX_PROTOCOL', 'EX_SOFTWARE', 'EX_TEMPFAIL', 'EX_UNAVAILABLE', 'EX_USAGE', 'F_OK', 'NGROUPS_MAX', 'O_APPEND', 'O_ASYNC', 'O_CREAT', 'O_DIRECT', 'O_DIRECTORY', 'O_DSYNC', 'O_EXCL', 'O_LARGEFILE', 'O_NDELAY', 'O_NOATIME', 'O_NOCTTY', 'O_NOFOLLOW', 'O_NONBLOCK', 'O_RDONLY', 'O_RDWR', 'O_RSYNC', 'O_SYNC', 'O_TRUNC', 'O_WRONLY', 'P_NOWAIT', 'P_NOWAITO', 'P_WAIT', 'R_OK', 'SEEK_CUR', 'SEEK_END', 'SEEK_SET', 'ST_APPEND', 'ST_MANDLOCK', 'ST_NOATIME', 'ST_NODEV', 'ST_NODIRATIME', 'ST_NOEXEC', 'ST_NOSUID', 'ST_RDONLY', 'ST_RELATIME', 'ST_SYNCHRONOUS', 'ST_WRITE', 'TMP_MAX', 'UserDict', 'WCONTINUED', 'WCOREDUMP', 'WEXITSTATUS', 'WIFCONTINUED', 'WIFEXITED', 'WIFSIGNALED', 'WIFSTOPPED', 'WNOHANG', 'WSTOPSIG', 'WTERMSIG', 'WUNTRACED', 'W_OK', 'X_OK', '_Environ', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_copy_reg', '_execvpe', '_exists', '_exit', '_get_exports_list', '_make_stat_result', '_make_statvfs_result', '_pickle_stat_result', '_pickle_statvfs_result', '_spawnvef', 'abort', 'access', 'altsep', 'chdir', 'chmod', 'chown', 'chroot', 'close', 'closerange', 'confstr', 'confstr_names', 'ctermid', 'curdir', 'defpath', 'devnull', 'dup', 'dup2', 'environ', 'errno', 'error', 'execl', 'execle', 'execlp', 'execlpe', 'execv', 'execve', 'execvp', 'execvpe', 'extsep', 'fchdir', 'fchmod', 'fchown', 'fdatasync', 'fdopen', 'fork', 'forkpty', 'fpathconf', 'fstat', 'fstatvfs', 'fsync', 'ftruncate', 'getcwd', 'getcwdu', 'getegid', 'getenv', 'geteuid', 'getgid', 'getgroups', 'getloadavg', 'getlogin', 'getpgid', 'getpgrp', 'getpid', 'getppid', 'getsid', 'getuid', 'isatty', 'kill', 'killpg', 'lchown', 'linesep', 'link', 'listdir', 'lseek', 'lstat', 'major', 'makedev', 'makedirs', 'minor', 'mkdir', 'mkfifo', 'mknod', 'name', 'nice', 'open', 'openpty', 'pardir', 'path', 'pathconf', 'pathconf_names', 'pathsep', 'pipe', 'popen', 'popen2', 'popen3', 'popen4', 'putenv', 'read', 'readlink', 'remove', 'removedirs', 'rename', 'renames', 'rmdir', 'sep', 'setegid', 'seteuid', 'setgid', 'setgroups', 'setpgid', 'setpgrp', 'setregid', 'setreuid', 'setsid', 'setuid', 'spawnl', 'spawnle', 'spawnlp', 'spawnlpe', 'spawnv', 'spawnve', 'spawnvp', 'spawnvpe', 'stat', 'stat_float_times', 'stat_result', 'statvfs', 'statvfs_result', 'strerror', 'symlink', 'sys', 'sysconf', 'sysconf_names', 'system', 'tcgetpgrp', 'tcsetpgrp', 'tempnam', 'times', 'tmpfile', 'tmpnam', 'ttyname', 'umask', 'uname', 'unlink', 'unsetenv', 'urandom', 'utime', 'wait', 'wait3', 'wait4', 'waitpid', 'walk', 'write']


ACL : How to enable read permission for /var/log/messages for ordinary user in Linux?


By default, /var/log/messages file can be accessed only by super user(root). The ordinary user does not even have read permission for this file.

# ls -l /var/log/messages
-rw------- 1 root root 658711 Apr 14 05:52 /var/log/messages

So how to enable read permission for an ordinary user, say xyz, for the file /var/log/messages?

Access Control Lists(acl) comes to the aid by allowing us to provide different levels of access to files and directories.

How to enable acl for Linux filesystem?


1) Install command line tool, acl, first. This package has Access Control List utilities.
   # yum install acl

2) Mount the partition with acl option enabled. Edit /etc/fstab as follows

UUID=fffff7aa-57b8-40aa-baa4-588c4eff7651   /  ext4    defaults,acl        1 1

3) Reboot the system for mount options to take effect.

Enable read access for user xyz for the file /var/log/messages


1) setfacl - Sets file access control list.
    
    # setfacl -m u:xyz:r /var/log/messages

2) Check the new file permissions for /var/log/messages
    
     # ls -l /var/log/messages
    -rw-r-----+ 1 root root 658711 Apr 14 05:52 /var/log/messages

Observe that now a + is observed at the end of file permissions.

3) Verify the access permissions for the file /var/log/messages using getfacl command

     getfacl - Get file access control list

  # getfacl /var/log/messages
  getfacl: Removing leading '/' from absolute path names
  # file: var/log/messages
  # owner: root
  # group: root
  user::rw-
  user:xyz:r--
  group::---
  mask::r--
  other::---

ext4 : mount options for ext4 file system in /etc/fstab

In /etc/fstab, usually the mount option is mentioned as defaults, like follows


UUID=fffff7aa-57b8-40aa-baa4-588c4eff7651          /              ext4    defaults        1 1
UUID=8b5a0a93-1dd3-4394-bb3e-0032a77201fa     /boot       ext4    defaults        1 2

What does this option defaults stand for in ext4 file system?

The default options for ext4 file system are: rw, suid, dev, exec, auto, nouser, async

Different file system mount options available are

auto       - Mount automatically at boot, or when the command mount -a is issued.
noauto   - Mount only when you tell it to.
exec      - Allow execution of binaries on the filesystem.
noexec  - Disallow execution of binaries on the filesystem.
ro         - Mount the filesystem read-only.
rw        - Mount the filesystem read-write.
user      - Allow any user to mount the filesystem. This automatically implies noexec, nosuid, nodev, unless overridden.
users    - Allow any user in the users group to mount the filesystem.
nouser  - Allow only root to mount the filesystem.
owner  - Allow the owner of device to mount.
sync     - I/O should be done synchronously.
async   - I/O should be done asynchronously.
dev      - Interpret block special devices on the filesystem.
nodev  - Don't interpret block special devices on the filesystem.
suid    - Allow the operation of suid, and sgid bits. They are mostly used to allow users on a computer system to execute binary executables with temporarily elevated privileges in order to perform a specific task.
nosuid - Block the operation of suid, and sgid bits.
noatime     - Don't update inode access times on the filesystem. Can help performance
nodiratime - Do not update directory inode access times on the filesystem. Can help performance 
relatime     - Update inode access times relative to modify or change time. Access time is only updated if the previous access time was earlier than the current modify or change time. Similar to noatime. Can help performance.
flush        - The vfat option to flush data more often, thus making copy dialogs or progress bars to stay up until all data is written  
acl         - Enable Access Control List(acl) for filesystem

Wednesday, April 3, 2013

Python : Loop through alphabets a to z

To loop through the alphabets a to z and print them, here is a simple script in python


#!/usr/bin/python

import string

alphabets = string.ascii_lowercase

for i in alphabets:
    print i

Output

a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z