Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <Ethernet.h>
- #include <string.h>
- // Enter a MAC address and IP address for your controller below.
- // The IP address will be dependent on your local network.
- // gateway and subnet are optional:
- byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x33, 0x75 };
- byte ip[] = { 192, 168, 1, 10 };
- byte gateway[] = { 192,168,1,1 };
- byte subnet[] = { 255, 255, 255, 0 };
- Server server(10050); // Zabbix agent listening port
- // Set vars
- String header= "ZBXD\1"; //Zabbix Header
- String input; //Define input
- String output; //Define output string
- String cmdout; //Command output string (for tests)
- long int datalen = 0; //Lenth of data output field
- //boolean gotAMessage = false; // whether or not you got a message from the client yet
- void setup() {
- // open the serial port
- Serial.begin(9600);
- Serial.println( "Arduino is starting up" );
- delay(3000);
- // initialize the ethernet device
- Serial.println( "Setting up the network");
- Ethernet.begin(mac, ip, gateway, subnet);
- // Start server
- Serial.println( "Starting listener");
- server.begin();
- // We're done
- Serial.println( "Startup is complete, listening for incoming requests");
- }
- long long endian_swap(unsigned int x) {
- x = (x>>24) |
- ((x<<8) & 0x00FF0000) |
- ((x>>8) & 0x0000FF00) |
- (x<<24);
- }
- void loop() {
- // listen for incoming clients
- Client client = server.available();
- if (client) {
- //boolean currentLineIsBlank = true;
- while (client.connected()) {
- if (client.available()) {
- char c = client.read();
- // Read input until newline
- if (c != '\n') {
- input += c;
- }
- //When newline reached, interpret the command
- if (c == '\n') {
- Serial.println(input);
- if ( input.indexOf("agent.ping") != -1) {
- cmdout = "1";
- }
- else {
- cmdout = "ZBX_NOTSUPPORTED";
- }
- datalen = cmdout.length();
- datalen = endian_swap(datalen);
- output += header;
- output.concat(datalen);
- output += cmdout;
- client.print(output);
- Serial.println(output);
- break;
- }
- }
- }
- // give the Zabbix Server time to receive the data
- delay(10);
- // close the connection:
- client.flush();
- client.stop();
- // Empty the strings for the next connection
- input = "";
- output = "";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement