Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
hardware:majak [2016/02/23 21:33]
admin created
hardware:majak [2016/02/28 01:45] (current)
admin
Line 2: Line 2:
  
 schema je jednoduche schema je jednoduche
-Client: +  * Client: 
-nagios zavola prikaz "echo "​nejaky problem"​ | netcat 10.0.5.5:​6555 ​+ ​nagios zavola prikaz "echo "​nejaky problem"​ | netcat 10.0.5.5:​6555 ​
  
-Server s majakem: +  * Server s majakem: 
-inetd posloucha na portu 6555 a spousti skript /​home/​majak/​bin/​majak.sh,​ ktery zapise na /​dev/​ttyUSB0 prikazy "​start_blink"​ nebo "​stop_blink"​+   ​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+====== ​Overit funkcnost majaku ​====== 
 + na strane serveru muzete na dvou kontrolnich mistech
 1. zapsat primo na /​dev/​ttyUSB0 1. zapsat primo na /​dev/​ttyUSB0
    echo start_blink > /​dev/​ttyUSB0    echo start_blink > /​dev/​ttyUSB0
 2. zapsat na localhost port 6555 2. zapsat na localhost port 6555
  
 +<​code>​
 root@cerna-ng:​~#​ telnet localhost 6555 root@cerna-ng:​~#​ telnet localhost 6555
 Trying ::1... Trying ::1...
Line 36: Line 38:
 root@cerna-ng:/​etc/​init.d# ​ root@cerna-ng:/​etc/​init.d# ​
 </​code>​ </​code>​
 +
 +====== Konfigurace nagiosu ======
 +<​code>​
 +define command {
 +        command_name ​   notify-by-majak
 +        command_line ​   /​usr/​bin/​printf "​%b"​ "Neco je $SERVICESTATE$"​ | nc 10.0.5.5 6555
 +        }
 +</​code>​
 +
 +====== Konfigurace Inetd.conf ======
 +<​code>​
 +6555    stream ​ tcp     ​nowait ​ majak   /​home/​majak/​bin/​majak.sh noarg
 +</​code>​
 +
 +====== 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.
 +
 +<code bash>
 +#!/bin/sh
 +
 +if grep -q CRITICAL; then
 +        cmd='​start_blink'​
 +else
 +        cmd='​stop_blink'​
 +fi
 +
 +echo $cmd >/​dev/​ttyUSB0
 +</​code>​
 +
 +
 +Na ''/​dev/​ttyUSB0''​ je pripojene arduino s timto programem
 +<code c>
 +/*
 +  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;
 +
 +  }
 +}
 +</​code>​
 +
 
hardware/majak.1456259603.txt.gz · Last modified: 2016/02/23 21:33 by admin