There's two parts to this post.

Capture & Logging

I'd like to automatically capture nmon data every 5 minutes for 24 hours and then rotate the file. I'd assume I would need to run the command below and have cron kick it off at 12:00am.

nmon -s 300 -c 288

Q1: How can I make sure this process does not continue to run before the next day kicks off and starts a new process?

Q2: Can I specify the file name to prevent someone manually running it and overwriting my log file? (ie hostname_YYYYMMDD.nmon)

Database Import

Once the file has been written, I'd like to import it into a mysql database.

I'm not sure if this can be done with a shell script, or if I need to use another language like Python.

I started writing the script below, but I seem to be stuck on a few areas. Please feel free to modify any mistakes found on what I have written so far.


# nmon_log_rot_db.sh

# Variables

# nmon
NMON = '/usr/local/bin/nmon/nmon'
# Log Directory
LOGDIR = '/var/log/nmon/'
# Hostname
HOST = '/etc/hostname'
# Date
DATE = '/usr/bin/date +%y%m%d'
# File Name
FILE = ${LOGDIR}/${HOST}-${DATE}.nmon

# Kill current nmon process
command to kill nmon_log_rot_db.sh

# Log to Database
# connect to database
HOSTNAME = mysqlhost;
DBUSER = dbuser;
DBPASS = dbpass;
DBNAME = dbname;

# network table
ROW_ID|datetime|hostname|iface|rx KB/s|tx KB/s

# cpu table

# mem table
ROW_ID|datetime|hostname|type (system or swap)|Total|Free|Used

# disk table

# Delete old log file
rm ${OLD_FILE}

# Start nmon
${NMON} -F ${FILE} -s 300 -c 288

The combination of s and c flags allows you to control the total duration of how long nmon is going to run in batch mode. Here's an example of running nmon for 24 hours, logging statistics every 60 seconds.

0 0 * * * /opt/nmon/nmon -f -s 60 -c 1440 -T -m /opt/nmon/nmon_logs/

In your case, the cron expression is going to log as follows:

0 0 * * * /opt/nmon/nmon -f -s 300 -c 288 -T -m /opt/nmon/nmon_logs/

The nmon binary automatically appends date to exported files so you should not worry about file name conflicts, however you probably want to ensure that older files are deleted after a while.

The default naming convention is hostname_yyMMdd_HHmm.log producing files like this: NURSWGHBS001_151113_2300.log

You can then import files into any tool than supports nmon format which is quite particular.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.