Nagios – Improve User Experience with Tampermonkey/Greasemonkey


Hello everyone, in these last few posts: Nagios – Change date format and Nagios – Use UTC I showed you how you can alter the date format and timezone of your Nagios server. Today I want to show you how you can improve User Experience with a simple Greasemonkey/Tampermonkey script by converting all date time values into relative time values (as seen on Facebook or used by SharePoint).

Important: If you want to use the below script without altering the “regexString” value you will need to follow this post of mine to make Nagios use the ISO8601 date format. And you need to follow this post to make Nagios use UTC.

Ok. Let´s get down to business 🙂

If you use Firefox install the Greasemonkey plugin or if you use Google Chrome install the Tampermonkey plugin to run the below script (If you use Internet Explorer you are unfortunately out of luck because neither Greasemonkey nor Tampermonkey are available for it to my knowledge). And here is the script:


// ==UserScript==
// @name Nagios - Date time to relative time
// @namespace https://theezitguy.wordpress.com/
// @version 1.0
// @description This script will turn every date time value into a relative time value (e.g. it will turn "2014-07-21 00:00:00" into "a year ago").
// @author theezitguy
// @match http://10.0.0.3/nagios/*
// @grant none
// @require http://momentjs.com/downloads/moment.js
// ==/UserScript==

//Get all textNodes of the website
textNodes = document.evaluate("//text()", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);

//Regex String - Search for date time values in ISO8601 format
var regexString = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/;
var searchRE = new RegExp(regexString);

//Go through all textNodes and replace date time values with relative time values
for (var i=0;i<textNodes.snapshotLength;i++)
{
var node = textNodes.snapshotItem(i);
node.data = node.data.replace(searchRE, moment.utc(node.data).fromNow());
}

The script will do the following:

It will go through every text node on the website and replace all date time values with relative time values.

Important: You need to alter the “@match” value to point to your Nagios server website before you can use it.

Here is a screenshot before I applied my script:

nagios_without_script_utc_time

And here is a screenshot after I applied my script:

nagios_with_script_relative_time

Well, that´s it. You can now easier read your data on your Nagios web interface.

Not bad, right? I hope you liked my today´s post and I hope to see you again next time 🙂

Sources:

http://momentjs.com/

http://forum.imerx.net/viewtopic.php?f=17&t=262&start=15

http://tuxradar.com/content/greasemonkey-beginners

http://www.digitoffee.com/programming/get-local-time-utc-using-moment-js/94/

Advertisements
Tagged

2 thoughts on “Nagios – Improve User Experience with Tampermonkey/Greasemonkey

  1. […] everyone, today I want to show you a little improvement I found for this previous post. (Btw This post has the same prerequisites. So have a look at my posts Nagios […]

  2. xsoft says:

    Thanks.

    Btw,
    change “moment.utc” to “moment.parseZone” and skip timezone issue.

    Also you can change include from “http://momentjs.com/downloads/moment.js”
    to “http://momentjs.com/downloads/moment-with-locales.min.js”
    and add “moment.locale(‘cs’);” to have it in your language
    (languages demo at bottom of http://momentjs.com/ )

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: