Przemek Wirkus / munin-client-lib

Dependents:   munin-display-board

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers munincli.h Source File

munincli.h

00001 // MUNIN Client library library
00002 // mbed Team     -   15 April 2014
00003 // Author: Przemyslaw Wirkus
00004 
00005 #ifndef MUNIN_CIENT_LIBRARY_H
00006 #define MUNIN_CIENT_LIBRARY_H
00007 
00008 #include <string>
00009 #include <vector>
00010 #include <sstream>
00011 #include "mbed.h"
00012 #include "EthernetInterface.h"
00013 
00014 namespace {
00015 // Munin command(s):
00016 const int MUNIN_BUFFER_SIZE = 256;
00017 }
00018 
00019 /** MuninClient class, used to fetch counters from Munin
00020 *
00021 * Example:
00022 * @code
00023 
00024 #include "munincli.h"
00025 #include "dispBoB.h"
00026 
00027 namespace
00028 {
00029 const char* MUNIN_SERVER_ADDRESS = "172.28.22.45";
00030 const int MUNIN_SERVER_PORT = 4949;
00031 }
00032 
00033 namespace {
00034 // Munin command(s):
00035 char CMD_FETCH_USERS[] = "fetch users\n";
00036 }
00037 
00038 
00039 int main()
00040 {
00041     MuninClient munin_lcd;
00042     munin_lcd.dhcp_connection();    // DHCP connection
00043     dispBoB bob_display(p28, p27, p26);
00044 
00045     while(true) {
00046         while (munin_lcd.socket_connection(MUNIN_SERVER_ADDRESS, MUNIN_SERVER_PORT) < 0) {
00047             wait(1);
00048         }
00049 
00050         munin_lcd.socket_recv();
00051         munin_lcd.socket_send(CMD_FETCH_USERS, sizeof(CMD_FETCH_USERS));
00052         munin_lcd.socket_recv_all();
00053         {
00054             const char *param = "usercount.value";
00055             const int counter_value = munin_lcd.get_param_value(param);
00056             bob_display.cls();
00057             bob_display.printf("% 6d\n", counter_value);
00058         }
00059 
00060         munin_lcd.socket_close();
00061         wait(3);
00062     }
00063 }
00064 
00065 
00066 * @endcode
00067 */
00068 class MuninClient
00069 {
00070 public:
00071     /** Connect to DHCP
00072     */
00073     void dhcp_connection() {
00074         printf("DHCP: Connecting ... ");
00075         eth.init(); //Use DHCP
00076         eth.connect();
00077         printf("%s\r\n", eth.getIPAddress());
00078     }
00079 
00080     /** Create a dispBoB object defined on the I2C master bus
00081     *
00082     * @param server Munin server IP / name
00083     * @param port Munin server port
00084     */
00085     int socket_connection(const char *server, const int port) {
00086         printf("TCP: Connecting ... ");
00087         int ret = socket.connect(server, port) < 0;
00088         printf("[OK]\r\n");
00089         return ret;
00090     }
00091 
00092     /** Closes socket conenction to Munin server
00093     */
00094     void socket_close() {
00095         socket.close();
00096     }
00097 
00098     /** Recv data from Munin to buffer 'buf'
00099     */
00100     int socket_recv() {
00101         const int n = socket.receive(buf, sizeof(buf) - 1);
00102         if (n > 0) {
00103             buf[n] = '\0';
00104         }
00105         return n;
00106     }
00107 
00108     /** Recv data from Munin to buffer 'buf'. Used to fetch reply for Munin command (given counter name)
00109     */
00110     int socket_recv_all() {
00111         const int n = socket.receive_all(buf, sizeof(buf) - 1);
00112         if (n > 0) {
00113             buf[n] = '\0';
00114         }
00115         return n;
00116     }
00117 
00118     /** Send data using socket to Munin server. Used to send command to Munin server.
00119     * @param cmd Munin command string, must end with \n
00120     * @param cmd_size Munin command buffer size
00121     */
00122     int socket_send(char *cmd, const int cmd_size) {
00123         const int n = socket.send(cmd, cmd_size - 1);
00124         return n;
00125     }
00126 
00127     /** Explodes Munin command reply into individual counters divided by '\n'.
00128     * Function extract counter value from conunter wirh name param_name.
00129     * @param param_name counter name
00130     */
00131     int get_param_value(std::string param_name) {
00132         // Explode Munin command and creaste separate strings with counters
00133         std::vector<std::string> s = explode(std::string(buf), '\n');
00134 
00135         // For each counter name get its name and if name matches param_name extract and return counter value
00136         for (std::vector<std::string>::iterator it = s.begin(); it != s.end(); ++it) {
00137             std::string &param = *it;
00138             if (!param.compare(0, param_name.size(), param_name)) {
00139                 std::vector<std::string> name_value = explode(param, ' ');
00140                 if (name_value.size() == 2) {
00141                     const int val = atoi(name_value[1].c_str());
00142                     return val;
00143                 }
00144             }
00145         }
00146         return 0;
00147     }
00148 
00149 protected:
00150     EthernetInterface eth;
00151     TCPSocketConnection socket;
00152     char buf[MUNIN_BUFFER_SIZE];
00153 
00154     // String explode function
00155     std::vector<std::string> explode(std::string const &s, char delim) {
00156         std::vector<std::string> result;
00157         std::istringstream iss(s);
00158         for (std::string token; std::getline(iss, token, delim); ) {
00159             result.push_back(token);
00160         }
00161         return result;
00162     }
00163 };
00164 
00165 #endif