Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.61 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <Ethernet.h>
  3. #include <string.h>
  4.  
  5. // Enter a MAC address and IP address for your controller below.
  6. // The IP address will be dependent on your local network.
  7. // gateway and subnet are optional:
  8. byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x33, 0x75 };  
  9. byte ip[] = { 192, 168, 1, 10 };    
  10. byte gateway[] = { 192,168,1,1 };
  11. byte subnet[] = { 255, 255, 255, 0 };
  12. Server server(10050);                       // Zabbix agent listening port
  13.  
  14. // Set vars
  15. String header= "ZBXD\1";                    //Zabbix Header
  16. String input;                               //Define input
  17. String output;                              //Define output string
  18. String cmdout;                              //Command output string (for tests)
  19. long int datalen = 0;                      //Lenth of data output field
  20.  
  21. //boolean gotAMessage = false; // whether or not you got a message from the client yet
  22.  
  23. void setup() {
  24.   // open the serial port
  25.   Serial.begin(9600);
  26.   Serial.println( "Arduino is starting up" );
  27.   delay(3000);
  28.   // initialize the ethernet device
  29.   Serial.println( "Setting up the network");
  30.   Ethernet.begin(mac, ip, gateway, subnet);
  31.   // Start server
  32.   Serial.println( "Starting listener");
  33.   server.begin();
  34.   // We're done
  35.   Serial.println( "Startup is complete, listening for incoming requests");
  36. }
  37.  
  38. long long endian_swap(unsigned int x) {
  39.    x = (x>>24) |
  40.    ((x<<8) & 0x00FF0000) |
  41.    ((x>>8) & 0x0000FF00) |
  42.    (x<<24);
  43. }
  44.  
  45. void loop() {
  46.   // listen for incoming clients
  47.   Client client = server.available();
  48.   if (client) {
  49.     //boolean currentLineIsBlank = true;
  50.     while (client.connected()) {
  51.       if (client.available()) {
  52.         char c = client.read();
  53.        
  54.         // Read input until newline
  55.         if (c != '\n') {
  56.           input += c;
  57.         }
  58.        
  59.         //When newline reached, interpret the command
  60.         if (c == '\n') {
  61.           Serial.println(input);
  62.           if ( input.indexOf("agent.ping") != -1) {
  63.            cmdout = "1";
  64.           }
  65.           else {
  66.             cmdout = "ZBX_NOTSUPPORTED";
  67.           }
  68.          
  69.           datalen = cmdout.length();
  70.           datalen = endian_swap(datalen);
  71.  
  72.           output += header;
  73.           output.concat(datalen);
  74.           output += cmdout;
  75.           client.print(output);
  76.           Serial.println(output);
  77.           break;
  78.         }
  79.       }
  80.     }
  81.     // give the Zabbix Server time to receive the data
  82.     delay(10);
  83.     // close the connection:
  84.     client.flush();
  85.     client.stop();
  86.     // Empty the strings for the next connection
  87.     input = "";
  88.     output = "";
  89.    }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement