2013년 1월 24일 목요일

Nagios: How to change datetime format of snmp trap

After I installed and Configured SNMPTT, saving snmpt trap data into table worked well.
Additionally, I needed to change the date format so that I could filter them easily by date and time.

By default, date format should be "Sun Dec 16 15:23:47 2012", but I want it to look like "2012-12-16 15:23:47"

When look at /etc/snmp/snmptt.ini, there are some properties related to datetime format.
$ vi /etc/snmp/snmptt.ini
...
# The date time format in strftime() format for the date/time when logging to 
# standard output, snmtt log files (log_file) and the unknown log file
# (unknown_trap_log_file). Defaults to localtime(). For SQL, see
# date_time_format_sql.
# Example:  %a %b %e %Y %H:%M:%S
# date_time_format = 
...
...
# The date time format for the traptime column in SQL, Defaults to 
# localtime(). When a date/time field is used in SQL, this should be 
# changed to follow a standard that is supported by the SQL server.
# Example:  For a MySQL DATETIME, use %Y-%m-%d %H:%M:%S.
# date_time_format_sql = 
...

As we can know from the above description, snmptt uses strftime() format.
The first format definition (date_time_format) is for log files. Generally, /var/log/snmptt/snmptt.log and /var/log/snmptt/snmpttunknown.log. The second definition (date_time_format_sql) is for traptime column of snmptt table in MySQL.

Before change the format in snmptt.ini, you can validate the format like the following:
$ mydate=`perl -e 'use POSIX; print strftime "%Y-%m-%d %T", localtime time;'`
$ echo $mydate
2013-01-22 10:19:45

Then uncomment those lines and change the formats in snmptt.ini
$ vi /etc/snmp/snmptt.ini
...
date_time_format = %Y-%m-%d %T
...
date_time_format_sql = %Y-%m-%d %T

# It needs to restart
$ service snmptt restart

Please visit the site to see various date time formats in strftime
http://my.safaribooksonline.com/book/operating-systems-and-server-administration/bash/0596526784/reference-lists/date_and_time_string_formatting_with_strftime

References:
1. http://stackoverflow.com/questions/13093709/how-to-use-shell-variables-in-perl-command-call-in-a-bash-shell-script

댓글 없음:

댓글 쓰기