Index: trunk/tools/nparser/nparser.cpp |
— | — | @@ -0,0 +1,118 @@ |
| 2 | +// Parser code for nagios |
| 3 | +// created by Petr Bena for wmf labs |
| 4 | + |
| 5 | +// licensed under gpl v 3 |
| 6 | + |
| 7 | + |
| 8 | +#include <iostream> |
| 9 | +#include <fstream> |
| 10 | +#include <stdio.h> |
| 11 | +#include <string> |
| 12 | +using namespace std; |
| 13 | + |
| 14 | +class instance |
| 15 | +{ |
| 16 | + public: |
| 17 | + string host; |
| 18 | + bool web; |
| 19 | + instance(string name) |
| 20 | + { |
| 21 | + host = name; |
| 22 | + web = false; |
| 23 | + } |
| 24 | +}; |
| 25 | + |
| 26 | +int main (int argc, char* argv[]) |
| 27 | +{ |
| 28 | + instance * config[8000]; |
| 29 | + if ( argc < 2 ) |
| 30 | + { |
| 31 | + printf ("Missing parameter!\n\n"); |
| 32 | + return 2; |
| 33 | + } |
| 34 | + string filepath( argv[1] ); |
| 35 | + filepath = filepath + "/novalist"; |
| 36 | + cout << filepath << endl; |
| 37 | + cout << "Parsing" << endl; |
| 38 | + ifstream data( (char*) filepath.c_str() ); |
| 39 | + string file; |
| 40 | + if (data.is_open()) |
| 41 | + { |
| 42 | + // we can process the data file |
| 43 | + while ( data.good() ) |
| 44 | + { |
| 45 | + string filepart; |
| 46 | + getline( data, filepart ); |
| 47 | + file = file + filepart; |
| 48 | + } |
| 49 | + data.close(); |
| 50 | + size_t pos; |
| 51 | + string x1("\"instance_name\""); |
| 52 | + string x2(""); |
| 53 | + string x3("generic::webserver::"); |
| 54 | + string x4("misc::apache"); |
| 55 | + int curr = 0; |
| 56 | + pos = file.find(x1); |
| 57 | + if (pos != string::npos) |
| 58 | + { |
| 59 | + bool done = false; |
| 60 | + pos = file.find(x1); |
| 61 | + file = file.substr(pos); |
| 62 | + while (!done) |
| 63 | + { |
| 64 | + string name = file.substr(x1.length() + 2); |
| 65 | + name = name.substr(0, name.find("\"", 4)); |
| 66 | + if ( name.find(" \"valueType") == string::npos ) |
| 67 | + { |
| 68 | + name = name.substr(1); |
| 69 | + cout << name << endl; |
| 70 | + config[curr] = new instance( name ); |
| 71 | + string data = file.substr(0, file.find("}")); |
| 72 | + if (data.find(x4) != string::npos || data.find(x3) != string::npos) |
| 73 | + { |
| 74 | + config[curr]->web=true; |
| 75 | + } |
| 76 | + curr++; |
| 77 | + } |
| 78 | + pos = file.find(x1, 20); |
| 79 | + if ( pos != string::npos ) |
| 80 | + { |
| 81 | + file = file.substr(pos); |
| 82 | + } else |
| 83 | + { |
| 84 | + done = true; |
| 85 | + } |
| 86 | + } |
| 87 | + string web_i; |
| 88 | + string ssh_i; |
| 89 | + int id = 0; |
| 90 | + while (id < curr) |
| 91 | + { |
| 92 | + if ( config[id]->web == true ) |
| 93 | + { |
| 94 | + web_i = web_i + config[id]->host + ","; |
| 95 | + } |
| 96 | + ssh_i = ssh_i + config[id]->host + ","; |
| 97 | + id++; |
| 98 | + } |
| 99 | + ssh_i = ssh_i.substr(0, ssh_i.length() -1 ); |
| 100 | + if (web_i.length() != 0) |
| 101 | + { |
| 102 | + web_i = web_i.substr(0, web_i.length() -1 ); |
| 103 | + } |
| 104 | + cout << "SSH:" << endl << ssh_i << endl; |
| 105 | + cout << "WWW:" << endl << web_i << endl; |
| 106 | + } |
| 107 | + else |
| 108 | + { |
| 109 | + cout << "Error: file is broken!" << endl; |
| 110 | + } |
| 111 | + } |
| 112 | + else |
| 113 | + { |
| 114 | + cout << "Error: unable to open the data file!" << endl; |
| 115 | + } |
| 116 | + |
| 117 | + return 0; |
| 118 | +} |
| 119 | + |
Index: trunk/tools/nparser/README |
— | — | @@ -0,0 +1,3 @@ |
| 2 | +This is a nagios parser for wmf labs, it parses the json output from mediawiki and generate config for nagios |
| 3 | + |
| 4 | +created by benapetr@gmail.com |