@@ -34,83 +34,80 @@ namespace ph = boost::placeholders;
3434
3535std::string gLog = " " ;
3636
37- int main (int argc, char * argv[]) {
38- PB::Commands::QueryResponseMessage response_message;
39- std::vector<std::string> args;
40- for (int i = 1 ; i < argc; i++) {
41- args.push_back (argv[i]);
42- }
43- PB::Commands::QueryRequestMessage request_message;
44- PB::Commands::QueryRequestMessage::Request *request = request_message.add_payload ();
45- request->set_command (" check_nrpe" );
46- for (int i = 1 ; i < argc; i++) {
47- request->add_arguments (argv[i]);
48- }
49-
50- check_nrpe client;
51- client.query (request_message, response_message);
52- NSCAPI::nagiosReturn ret = NSCAPI::query_return_codes::returnOK;
53- for (const ::PB::Commands::QueryResponseMessage_Response &response: response_message.payload ()) {
54- ret = nscapi::plugin_helper::maxState (ret, nscapi::protobuf::functions::gbp_to_nagios_status (response.result ()));
55- for (const ::PB::Commands::QueryResponseMessage_Response_Line &line: response.lines ()) {
56- std::cout << line.message ();
57- std::string tmp = nscapi::protobuf::functions::build_performance_data (line, nscapi::protobuf::functions::no_truncation);
58- if (!tmp.empty ())
59- std::cout << ' |' << tmp;
60- }
61- }
62- return ret;
37+ int main (int argc, char *argv[]) {
38+ PB::Commands::QueryResponseMessage response_message;
39+ std::vector<std::string> args;
40+ for (int i = 1 ; i < argc; i++) {
41+ args.push_back (argv[i]);
42+ }
43+ PB::Commands::QueryRequestMessage request_message;
44+ PB::Commands::QueryRequestMessage::Request *request = request_message.add_payload ();
45+ request->set_command (" check_nrpe" );
46+ for (int i = 1 ; i < argc; i++) {
47+ request->add_arguments (argv[i]);
48+ }
49+
50+ check_nrpe client;
51+ client.query (request_message, response_message);
52+ NSCAPI::nagiosReturn ret = NSCAPI::query_return_codes::returnOK;
53+ for (const ::PB::Commands::QueryResponseMessage_Response &response : response_message.payload ()) {
54+ ret = nscapi::plugin_helper::maxState (ret, nscapi::protobuf::functions::gbp_to_nagios_status (response.result ()));
55+ for (const ::PB::Commands::QueryResponseMessage_Response_Line &line : response.lines ()) {
56+ std::cout << line.message ();
57+ std::string tmp = nscapi::protobuf::functions::build_performance_data (line, nscapi::protobuf::functions::no_truncation);
58+ if (!tmp.empty ()) std::cout << ' |' << tmp;
59+ }
60+ }
61+ return ret;
6362}
6463
6564#ifdef WIN32
6665boost::filesystem::path get_selfpath () {
67- wchar_t buff[4096 ];
68- if (GetModuleFileName (NULL , buff, sizeof (buff) - 1 )) {
69- boost::filesystem::path p = std::wstring (buff);
70- return p.parent_path ();
71- }
72- return boost::filesystem::initial_path ();
66+ wchar_t buff[4096 ];
67+ if (GetModuleFileName (NULL , buff, sizeof (buff) - 1 )) {
68+ boost::filesystem::path p = std::wstring (buff);
69+ return p.parent_path ();
70+ }
71+ return boost::filesystem::initial_path ();
7372}
7473#else
7574boost::filesystem::path get_selfpath () {
76- char buff[1024 ];
77- ssize_t len = ::readlink (" /proc/self/exe" , buff, sizeof (buff) - 1 );
78- if (len != -1 ) {
79- buff[len] = ' \0 ' ;
80- boost::filesystem::path p = std::string (buff);
81- return p.parent_path ();
82- }
83- return boost::filesystem::initial_path ();
75+ char buff[1024 ];
76+ ssize_t len = ::readlink (" /proc/self/exe" , buff, sizeof (buff) - 1 );
77+ if (len != -1 ) {
78+ buff[len] = ' \0 ' ;
79+ boost::filesystem::path p = std::string (buff);
80+ return p.parent_path ();
81+ }
82+ return boost::filesystem::initial_path ();
8483}
8584#endif
86- boost::filesystem::path getBasePath (void ) {
87- return get_selfpath ();
88- }
85+ boost::filesystem::path getBasePath (void ) { return get_selfpath (); }
8986
9087#ifdef WIN32
91- typedef DWORD (WINAPI *PFGetTempPath)(__in DWORD nBufferLength, __out LPTSTR lpBuffer);
88+ typedef DWORD (WINAPI *PFGetTempPath)(__in DWORD nBufferLength, __out LPTSTR lpBuffer);
9289#endif
9390boost::filesystem::path getTempPath () {
94- std::string tempPath;
91+ std::string tempPath;
9592#ifdef WIN32
96- unsigned int buf_len = 4096 ;
97- HMODULE hKernel = ::LoadLibrary (L" kernel32" );
98- if (hKernel) {
99- // Find PSAPI functions
100- PFGetTempPath FGetTempPath = (PFGetTempPath)::GetProcAddress (hKernel, " GetTempPathW" );
101- if (FGetTempPath) {
102- wchar_t * buffer = new wchar_t [buf_len + 1 ];
103- if (FGetTempPath (buf_len, buffer)) {
104- std::wstring s = buffer;
105- tempPath = utf8::cvt<std::string>(s);
106- }
107- delete[] buffer;
108- }
109- }
93+ unsigned int buf_len = 4096 ;
94+ HMODULE hKernel = ::LoadLibrary (L" kernel32" );
95+ if (hKernel) {
96+ // Find PSAPI functions
97+ PFGetTempPath FGetTempPath = (PFGetTempPath)::GetProcAddress (hKernel, " GetTempPathW" );
98+ if (FGetTempPath) {
99+ wchar_t * buffer = new wchar_t [buf_len + 1 ];
100+ if (FGetTempPath (buf_len, buffer)) {
101+ std::wstring s = buffer;
102+ tempPath = utf8::cvt<std::string>(s);
103+ }
104+ delete[] buffer;
105+ }
106+ }
110107#else
111- tempPath = " /tmp" ;
108+ tempPath = " /tmp" ;
112109#endif
113- return tempPath;
110+ return tempPath;
114111}
115112#ifdef WIN32
116113#ifndef CSIDL_COMMON_APPDATA
@@ -119,108 +116,100 @@ boost::filesystem::path getTempPath() {
119116typedef BOOL (WINAPI *fnSHGetSpecialFolderPath)(HWND hwndOwner, LPTSTR lpszPath, int nFolder, BOOL fCreate );
120117
121118__inline BOOL WINAPI _SHGetSpecialFolderPath (HWND hwndOwner, LPTSTR lpszPath, int nFolder, BOOL fCreate ) {
122- static fnSHGetSpecialFolderPath __SHGetSpecialFolderPath = NULL ;
123- if (!__SHGetSpecialFolderPath) {
124- HMODULE hDLL = LoadLibrary (L" shfolder.dll" );
125- if (hDLL != NULL )
126- __SHGetSpecialFolderPath = (fnSHGetSpecialFolderPath)GetProcAddress (hDLL, " SHGetSpecialFolderPathW" );
127- }
128- if (__SHGetSpecialFolderPath)
129- return __SHGetSpecialFolderPath (hwndOwner, lpszPath, nFolder, fCreate );
130- return FALSE ;
119+ static fnSHGetSpecialFolderPath __SHGetSpecialFolderPath = NULL ;
120+ if (!__SHGetSpecialFolderPath) {
121+ HMODULE hDLL = LoadLibrary (L" shfolder.dll" );
122+ if (hDLL != NULL ) __SHGetSpecialFolderPath = (fnSHGetSpecialFolderPath)GetProcAddress (hDLL, " SHGetSpecialFolderPathW" );
123+ }
124+ if (__SHGetSpecialFolderPath) return __SHGetSpecialFolderPath (hwndOwner, lpszPath, nFolder, fCreate );
125+ return FALSE ;
131126}
132127#endif
133128
134129struct stdout_client_handler : public socket_helpers ::client::client_handler {
135- void log_debug (std::string, int , std::string msg) const {
136- if (gLog == " debug" )
137- std::cout << msg << std::endl;
138- }
139- void log_error (std::string, int , std::string msg) const {
140- if (gLog == " debug" || gLog == " error" )
141- std::cout << msg << std::endl;
142- }
143-
144- std::string getFolder (std::string key) {
145- std::string default_value = getBasePath ().string ();
146- if (key == " certificate-path" ) {
147- default_value = CERT_FOLDER;
148- } else if (key == " module-path" ) {
149- default_value = MODULE_FOLDER;
150- } else if (key == " web-path" ) {
151- default_value = WEB_FOLDER;
152- } else if (key == " scripts" ) {
153- default_value = SCRIPTS_FOLDER;
154- } else if (key == CACHE_FOLDER_KEY) {
155- default_value = DEFAULT_CACHE_PATH;
156- } else if (key == CRASH_ARCHIVE_FOLDER_KEY) {
157- default_value = CRASH_ARCHIVE_FOLDER;
158- } else if (key == " base-path" ) {
159- default_value = getBasePath ().string ();
160- } else if (key == " temp" ) {
161- default_value = getTempPath ().string ();
162- } else if (key == " shared-path" || key == " base-path" || key == " exe-path" ) {
163- default_value = getBasePath ().string ();
164- }
130+ void log_debug (std::string, int , std::string msg) const {
131+ if (gLog == " debug" ) std::cout << msg << std::endl;
132+ }
133+ void log_error (std::string, int , std::string msg) const {
134+ if (gLog == " debug" || gLog == " error" ) std::cout << msg << std::endl;
135+ }
136+
137+ std::string getFolder (std::string key) {
138+ std::string default_value = getBasePath ().string ();
139+ if (key == " certificate-path" ) {
140+ default_value = CERT_FOLDER;
141+ } else if (key == " module-path" ) {
142+ default_value = MODULE_FOLDER;
143+ } else if (key == " web-path" ) {
144+ default_value = WEB_FOLDER;
145+ } else if (key == " scripts" ) {
146+ default_value = SCRIPTS_FOLDER;
147+ } else if (key == CACHE_FOLDER_KEY) {
148+ default_value = DEFAULT_CACHE_PATH;
149+ } else if (key == CRASH_ARCHIVE_FOLDER_KEY) {
150+ default_value = CRASH_ARCHIVE_FOLDER;
151+ } else if (key == " base-path" ) {
152+ default_value = getBasePath ().string ();
153+ } else if (key == " temp" ) {
154+ default_value = getTempPath ().string ();
155+ } else if (key == " shared-path" || key == " base-path" || key == " exe-path" ) {
156+ default_value = getBasePath ().string ();
157+ }
165158#ifdef WIN32
166- else if (key == " common-appdata" ) {
167- wchar_t buf[MAX_PATH + 1 ];
168- if (_SHGetSpecialFolderPath (NULL , buf, CSIDL_COMMON_APPDATA, FALSE ))
169- default_value = utf8::cvt<std::string>(buf);
170- else
171- default_value = getBasePath ().string ();
172- }
159+ else if (key == " common-appdata" ) {
160+ wchar_t buf[MAX_PATH + 1 ];
161+ if (_SHGetSpecialFolderPath (NULL , buf, CSIDL_COMMON_APPDATA, FALSE ))
162+ default_value = utf8::cvt<std::string>(buf);
163+ else
164+ default_value = getBasePath ().string ();
165+ }
173166#else
174- else if (key == " etc" ) {
175- default_value = " /etc" ;
176- }
167+ else if (key == " etc" ) {
168+ default_value = " /etc" ;
169+ }
177170#endif
178- return default_value;
179- }
180-
181- std::string expand_path (std::string file) {
182- std::string::size_type pos = file.find (' $' );
183- while (pos != std::string::npos) {
184- std::string::size_type pstart = file.find (' {' , pos);
185- std::string::size_type pend = file.find (' }' , pstart);
186- std::string key = file.substr (pstart + 1 , pend - 2 );
187-
188- std::string tmp = file;
189- str::utils::replace (file, " ${" + key + " }" , getFolder (key));
190- if (file == tmp)
191- pos = file.find_first_of (' $' , pos + 1 );
192- else
193- pos = file.find_first_of (' $' );
194- }
195- return file;
196- }
171+ return default_value;
172+ }
173+
174+ std::string expand_path (std::string file) {
175+ std::string::size_type pos = file.find (' $' );
176+ while (pos != std::string::npos) {
177+ std::string::size_type pstart = file.find (' {' , pos);
178+ std::string::size_type pend = file.find (' }' , pstart);
179+ std::string key = file.substr (pstart + 1 , pend - 2 );
180+
181+ std::string tmp = file;
182+ str::utils::replace (file, " ${" + key + " }" , getFolder (key));
183+ if (file == tmp)
184+ pos = file.find_first_of (' $' , pos + 1 );
185+ else
186+ pos = file.find_first_of (' $' );
187+ }
188+ return file;
189+ }
197190};
198191
199192bool test (client::destination_container &source, client::destination_container &) {
200- if (source.has_data (" log" ))
201- gLog = source.get_string_data (" log" );
202- else
203- gLog = " error" ;
204- return true ;
193+ if (source.has_data (" log" ))
194+ gLog = source.get_string_data (" log" );
195+ else
196+ gLog = " error" ;
197+ return true ;
205198}
206199
207200boost::program_options::options_description add_client_options (client::destination_container &source, client::destination_container &) {
208- namespace po = boost::program_options;
209-
210- po::options_description desc (" Client options" );
211- desc.add_options ()
212- (" log" , po::value<std::string>()->notifier (boost::bind (&client::destination_container::set_string_data, &source, " log" , ph::_1)),
213- " Set log level" )
214- ;
215- return desc;
201+ namespace po = boost::program_options;
202+
203+ po::options_description desc (" Client options" );
204+ desc.add_options ()(" log" , po::value<std::string>()->notifier (boost::bind (&client::destination_container::set_string_data, &source, " log" , ph::_1)),
205+ " Set log level" );
206+ return desc;
216207}
217208
218209typedef nrpe_client::nrpe_client_handler<stdout_client_handler> nrpe_client_handler;
219210check_nrpe::check_nrpe () : client_(" nrpe" , boost::make_shared<nrpe_client_handler>(), boost::make_shared<nrpe_handler::options_reader_impl>()) {
220- client_.client_desc = &add_client_options;
221- client_.client_pre = &test;
211+ client_.client_desc = &add_client_options;
212+ client_.client_pre = &test;
222213}
223214
224- void check_nrpe::query (const PB::Commands::QueryRequestMessage &request, PB::Commands::QueryResponseMessage &response) {
225- client_.do_query (request, response);
226- }
215+ void check_nrpe::query (const PB::Commands::QueryRequestMessage &request, PB::Commands::QueryResponseMessage &response) { client_.do_query (request, response); }
0 commit comments