Table of Contents

Jak funguje nas poplasny Nagios Majak

schema je jednoduche

nagios zavola prikaz “echo “nejaky problem” | netcat 10.0.5.5:6555

inetd posloucha na portu 6555 a spousti skript /home/majak/bin/majak.sh, ktery zapise na /dev/ttyUSB0 prikazy “start_blink” nebo “stop_blink”

Overit funkcnost majaku

na strane serveru muzete na dvou kontrolnich mistech 1. zapsat primo na /dev/ttyUSB0

 echo start_blink > /dev/ttyUSB0

2. zapsat na localhost port 6555

root@cerna-ng:~# telnet localhost 6555
Trying ::1...
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
nagios vola, nejaky CRITICAL problem
Connection closed by foreign host.
root@cerna-ng:~#
root@cerna-ng:/root# telnet localhost 6555
Trying ::1...
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
libovolny text, neobsahujici slovo c-r-i-t-i-c-a-l
^]
telnet> close
Connection closed.
root@cerna-ng:/etc/init.d# 

Konfigurace nagiosu

define command {
        command_name    notify-by-majak
        command_line    /usr/bin/printf "%b" "Neco je $SERVICESTATE$" | nc 10.0.5.5 6555
        }

Konfigurace Inetd.conf

6555    stream  tcp     nowait  majak   /home/majak/bin/majak.sh noarg

Poslouchajici skript majak.sh

Musite pridat uzivatele majak a napriklad na Debianu ho jeste dat do skupiny 'dialout', protoze jinak nema prava sahat na /dev/ttyUSB0.

#!/bin/sh
 
if grep -q CRITICAL; then
        cmd='start_blink'
else
        cmd='stop_blink'
fi
 
echo $cmd >/dev/ttyUSB0

Na /dev/ttyUSB0 je pripojene arduino s timto programem

/*
  Majacek - Nagios 
 */
 
#define  MAX_INPUT_STR  200
#define  START_BLINK 0
#define  STOP_BLINK 1
#define  USER_STOP  2
 
String input_str = "";         // a string to hold incoming serial data
volatile boolean string_complete = false;  // whether the string is complete
 
const String cmds[] = {
  "start_blink",
  "stop_blink",
  "user_stop",
  "",
  ""};
 
int relay = 3;
int led = 13;
 
 
 
void setup() {
  Serial.begin(9600);
  pinMode(relay, OUTPUT);
  pinMode(led, OUTPUT);
  digitalWrite(relay, HIGH);
  digitalWrite(led, LOW);
 
// 3x blikni, at vim, ze jsi ok :)
  digitalWrite(led, HIGH);
  delay(400);
  digitalWrite(led, LOW);
  delay(200);
  digitalWrite(led, HIGH);
  delay(400);
  digitalWrite(led, LOW);
  delay(200);
  digitalWrite(led, HIGH);
  delay(400);
  digitalWrite(led, LOW);  
  input_str.reserve(MAX_INPUT_STR);
 
//  Serial.println("Initialized.");
}
 
 
int parse_input(String s) {
  int i = 0;
 
  while (cmds[i] != "") {
//   Serial.print("Debug parse_input cmd: ");
//   Serial.println(cmds[i]);
    if (s == cmds[i])
       break;
    i++;
  }
 
  return i;
}
 
void start_blink() {
   digitalWrite(relay, LOW);
   digitalWrite(led, HIGH);
}
 
void stop_blink() {
   digitalWrite(relay, HIGH);
   digitalWrite(led, LOW);
}
 
 
void loop() {
 
  if (string_complete) {
//    Serial.println(input_str); 
 
    int cmd = parse_input(input_str);
    switch (cmd) {
      case START_BLINK:
        start_blink();
        break;
      case STOP_BLINK:
        stop_blink();
        break;
      case USER_STOP:
        stop_blink();
        break;
      default:
//        Serial.print("ERROR: Unknown command: ");
//        Serial.println(input_str);
        ;
    }
 
    input_str = "";
    string_complete = false;
  }
 
}
 
 
/* some debug
    Serial.print("This is the input char:");
    int ord = in_char;
    int enter = '\n';
    Serial.print(enter);
    Serial.println(ord);
*/
 
void serialEvent() {
  while (Serial.available()) {
    char in_char = (char)Serial.read(); 
 
 
    if (in_char == '\n')
      string_complete = true;
    else
      input_str += in_char;
 
  }
}