Advertisement
genBTC

nagios Regex audio alerts tampermonkey

Jun 25th, 2017
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name Nagios Audio alerts
  3. // @version 1.0
  4. // @author genBTC
  5. // @namespace https://github.com/genbtc/
  6. // @description This script will string and time match stuff and play audio files as alerts
  7. // @match https://10.0.0.3/your/Nagios/Page/AggregatePage/HasToBeThatPage/*
  8. // @grant none
  9. // @require https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js
  10. // ==/UserScript==
  11. var startupDelay = 1000;    //How long to wait for everything to load (in ms)
  12. var runInterval = 10000;      //How often to loop through logic (in ms) (also minimum time between different beeps)
  13.  
  14. //initialize player with sound
  15. var player = document.createElement('audio');
  16. player.src = 'https://dl.dropbox.com/u/7079101/coin.mp3';
  17. player.preload = 'auto';
  18.  
  19. //run the init, and the main Loop
  20. setTimeout(delayStart, startupDelay);
  21. //set up the loop
  22. function delayStart() {
  23.     initialize();
  24.     setInterval(mainLoop, runInterval);
  25. }
  26.  
  27. //Main:
  28. function mainLoop() {
  29.     //Grab all the columnar elements that correspond to being lit up by the statusCritical CSS style
  30.     var criticalClass = "statusCritical";    
  31.     var getcritical = document.getElementsByClassName(criticalClass);
  32.     //Feed the 7 columns into a new group that stores it as the whole line.
  33.     var criticalLines = [];
  34.     for (var i=0;i<getcritical.length;i+=7) {
  35.         criticalLines.push(getcritical[i].parentNode);
  36.     }
  37.     //At this point we have a certain number of lines that are Critical that need to be dealt with. Need to be matched.
  38.     //Loop through all Critical lines one at a time:
  39.     for (var i=0;i<criticalLines.length;i++) {
  40.         var columns = criticalLines[i];
  41.         var time = columns[5].split(/[ ,]+/);    //split on whitespace.
  42.         //create a moment.js duration object that is initialized with all the right fields (the slice is to cut out the last letter.)
  43.         var minutes = moment.duration({
  44.                         seconds: time[3].slice(0, -1),
  45.                         minutes: time[2].slice(0, -1),
  46.                         hours: time[1].slice(0, -1),
  47.                         days: time[0].slice(0, -1)
  48.                     }).asMinutes();
  49.         //if the minutes arent at 20 yet, skip.
  50.         if (minutes < 20) return;
  51.             // double check that the line has the word CRITICAL in column [4].
  52.             // triple check to make sure we didnt already just alert on this, by checking the field we wrote.        
  53.         if (columns[4].innerHTML.includes("CRITICAL") && !columns[3].innerHTML.includes("ALERTED!")) {
  54.             //play the audio.
  55.             player.play();
  56.             // write some data to that blank field, to prove to both of us we alerted on it.
  57.             columns[3].innerHTML = "ALERTED";
  58.         }
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement