Changeset 7354aa1 in nscp


Ignore:
Timestamp:
01/09/12 22:34:25 (17 months ago)
Author:
Michael Medin <michael@…>
Branches:
master, 0.4.0, 0.4.1, 0.4.2
Children:
ba63b95
Parents:
a87ce04
Message:
  • Fixed hostname issue in NSCAClient (hostname = auto now auto detects hostname
  • Fixed some minor issues in NSCA and CLI api
  • Added CLI based (exec) APIs to NSCA unit tests (now 697 test for NSCA)
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • changelog

    ra87ce04 r7354aa1  
    66 * Fix RtlStringFromGUID problem on NT4 
    77 
     82012-01-09 MickeM 
     9 * Fixed hostname issue in NSCAClient (hostname = auto now auto detects hostname 
     10 * Fixed some minor issues in NSCA and CLI api 
     11 * Added CLI based (exec) APIs to NSCA unit tests (now 697 test for NSCA) 
    812 
    9132012-01-08 MickeM 
  • include/client/command_line_parser.cpp

    ra87ce04 r7354aa1  
    2525  desc.add_options() 
    2626    ("command,c", po::wvalue<std::wstring>(&command_data->command), "The name of the command that the remote daemon should run") 
     27    ("alias,a", po::wvalue<std::wstring>(&command_data->command), "Same as command") 
    2728    ("message,m", po::wvalue<std::wstring>(&command_data->message), "Message") 
    28     ("result,r", po::value<unsigned int>(&command_data->result), "Result code") 
     29    ("result,r", po::wvalue<std::wstring>(&command_data->result), "Result code either a number or OK, WARN, CRIT, UNKNOWN") 
    2930    ; 
    3031} 
     
    5253} 
    5354 
     55int parse_result(std::wstring key) { 
     56  if (key == _T("UNKNOWN") || key == _T("unknown")) 
     57    return NSCAPI::returnUNKNOWN; 
     58  if (key == _T("warn") || key == _T("WARN") || key == _T("WARNING") || key == _T("warning")) 
     59    return NSCAPI::returnWARN; 
     60  if (key == _T("crit") || key == _T("CRIT") || key == _T("CRITICAL") || key == _T("critical")) 
     61    return NSCAPI::returnWARN; 
     62  if (key == _T("OK") || key == _T("ok")) 
     63    return NSCAPI::returnUNKNOWN; 
     64  try { 
     65    return strEx::stoi(key); 
     66  } catch (...) { 
     67    return NSCAPI::returnUNKNOWN; 
     68  } 
     69} 
    5470 
    5571int client::command_line_parser::do_execute_command_as_exec(configuration &config, const std::wstring &command, std::list<std::wstring> &arguments, std::string &result) { 
     
    226242  modify_header(config, message.mutable_header(), config.data->recipient); 
    227243  message.set_channel("CLI"); 
    228   nscapi::functions::append_simple_submit_request_payload(message.add_payload(), config.data->command, config.data->result, config.data->message); 
     244  nscapi::functions::append_simple_submit_request_payload(message.add_payload(), config.data->command, parse_result(config.data->result), config.data->message); 
    229245 
    230246  return config.handler->submit(config.data, message.mutable_header(), message.SerializeAsString(), result); 
  • include/client/command_line_parser.hpp

    ra87ce04 r7354aa1  
    3232    std::wstring command_line; 
    3333    std::wstring message; 
    34     unsigned int result; 
     34    std::wstring result; 
    3535    std::vector<std::wstring> arguments; 
    3636 
  • include/nscapi/functions.hpp

    ra87ce04 r7354aa1  
    347347      exec_response_message.ParseFromString(data); 
    348348      Plugin::QueryResponseMessage query_response_message; 
    349       query_response_message.mutable_header()->CopyFrom(exec_response_message); 
     349      query_response_message.mutable_header()->CopyFrom(exec_response_message.header()); 
    350350      for (int i=0;i<exec_response_message.payload_size();++i) { 
    351351        Plugin::ExecuteResponseMessage::Response p = exec_response_message.payload(i); 
     
    358358      submit_response_message.ParseFromString(data); 
    359359      Plugin::QueryResponseMessage query_response_message; 
    360       query_response_message.mutable_header()->CopyFrom(submit_response_message); 
     360      query_response_message.mutable_header()->CopyFrom(submit_response_message.header()); 
    361361      for (int i=0;i<submit_response_message.payload_size();++i) { 
    362362        Plugin::SubmitResponseMessage::Response p = submit_response_message.payload(i); 
     
    370370      submit_response_message.ParseFromString(data); 
    371371      Plugin::ExecuteResponseMessage exec_response_message; 
    372       exec_response_message.mutable_header()->CopyFrom(submit_response_message); 
     372      exec_response_message.mutable_header()->CopyFrom(submit_response_message.header()); 
    373373      for (int i=0;i<submit_response_message.payload_size();++i) { 
    374374        Plugin::SubmitResponseMessage::Response p = submit_response_message.payload(i); 
     
    381381      query_response_message.ParseFromString(data); 
    382382      Plugin::ExecuteResponseMessage exec_response_message; 
    383       exec_response_message.mutable_header()->CopyFrom(query_response_message); 
     383      exec_response_message.mutable_header()->CopyFrom(query_response_message.header()); 
    384384      for (int i=0;i<query_response_message.payload_size();++i) { 
    385385        Plugin::QueryResponseMessage::Response p = query_response_message.payload(i); 
  • modules/NSCAClient/NSCAClient.cpp

    ra87ce04 r7354aa1  
    8080 
    8181    settings.alias().add_key_to_settings() 
    82       (_T("hostname"), sh::string_key(&hostname_), 
     82      (_T("hostname"), sh::string_key(&hostname_, "auto"), 
    8383      _T("HOSTNAME"), _T("The host name of this host if set to blank (default) the windows name of the computer will be used.")) 
    8484 
     
    122122 
    123123    get_core()->registerSubmissionListener(get_id(), channel_); 
     124 
     125    if (hostname_ == "auto") { 
     126      hostname_ = boost::asio::ip::host_name(); 
     127 
     128    } 
    124129 
    125130    if (!targets.has_target(_T("default"))) { 
     
    224229NSCAPI::nagiosReturn NSCAAgent::handleRAWCommand(const wchar_t* char_command, const std::string &request, std::string &result) { 
    225230  nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_query_request(char_command, request); 
    226   std::wstring cmd = client::command_line_parser::parse_command(data.command, _T("syslog")); 
     231  std::wstring cmd = client::command_line_parser::parse_command(data.command, _T("nsca")); 
    227232  client::configuration config; 
    228233  setup(config); 
     
    234239NSCAPI::nagiosReturn NSCAAgent::commandRAWLineExec(const wchar_t* char_command, const std::string &request, std::string &result) { 
    235240  nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_exec_request(char_command, request); 
    236   std::wstring cmd = client::command_line_parser::parse_command(char_command, _T("syslog")); 
     241  std::wstring cmd = client::command_line_parser::parse_command(char_command, _T("nsca")); 
    237242  if (!client::command_line_parser::is_command(cmd)) 
    238243    return NSCAPI::returnIgnored; 
  • modules/NSCAClient/NSCAClient.h

    ra87ce04 r7354aa1  
    6868      host = url.host; 
    6969      port = url.get_port(); 
    70       sender_hostname = sender.get_string_data("host"); 
     70      sender_hostname = sender.host; 
     71      if (sender.has_data("host")) 
     72        sender_hostname = sender.get_string_data("host"); 
    7173    } 
    7274    unsigned int get_encryption() { 
  • scripts/python/test_nsca.py

    ra87ce04 r7354aa1  
    121121    None 
    122122     
    123   def submit_payload(self, encryption, source, status, msg, perf): 
     123  def wait_and_validate(self, uuid, result, msg, perf, tag): 
     124    found = False 
     125    for i in range(0,10): 
     126      if self.has_response(uuid): 
     127        rmsg = self.get_response(uuid) 
     128        result.add_message(rmsg.got_response, 'Testing to recieve message using %s'%tag) 
     129        result.add_message(rmsg.got_simple_response, 'Testing to recieve simple message using %s'%tag) 
     130        #result.assert_equals(rmsg.last_source, source, 'Verify that source is sent through') 
     131        result.assert_equals(rmsg.command, uuid, 'Verify that command is sent through using %s'%tag) 
     132        result.assert_contains(rmsg.message, msg, 'Verify that message is sent through using %s'%tag) 
     133        result.assert_equals(rmsg.perfdata, perf, 'Verify that performance data is sent through using %s'%tag) 
     134        self.del_response(uuid) 
     135        return True 
     136      else: 
     137        log('Waiting for %s (%d/10)'%(uuid, i+1)) 
     138        sleep(1) 
     139    result.add_message(False, 'Failed to recieve message %s using %s'%(uuid, tag)) 
     140    return False 
     141   
     142     
     143  def submit_payload(self, encryption, source, status, msg, perf, tag, retry=True): 
    124144    message = plugin_pb2.SubmitRequestMessage() 
    125145     
     
    144164    payload.source = source 
    145165    (result_code, err) = core.submit('nsca_test_outbox', message.SerializeToString()) 
    146     result = TestResult() 
    147      
    148     found = False 
    149     for i in range(0,10): 
    150       if self.has_response(uid): 
    151         rmsg = self.get_response(uid) 
    152         result.add_message(rmsg.got_response, 'Testing to recieve message using %s'%encryption) 
    153         result.add_message(rmsg.got_simple_response, 'Testing to recieve simple message using %s'%encryption) 
    154         result.add_message(len(err) == 0, 'Testing to send message using %s'%encryption, err) 
    155         #result.assert_equals(rmsg.last_source, source, 'Verify that source is sent through') 
    156         result.assert_equals(rmsg.command, uid, 'Verify that command is sent through') 
    157         result.assert_contains(rmsg.message, msg, 'Verify that message is sent through') 
    158         result.assert_equals(rmsg.perfdata, perf, 'Verify that performance data is sent through') 
    159         self.del_response(uid) 
    160         found = True 
    161         break 
    162       else: 
    163         log('Waiting for %s'%uid) 
    164         sleep(1) 
    165     if not found: 
    166       return None 
     166 
     167    result = TestResult() 
     168    result.add_message(len(err) == 0, 'Testing to send message using %s/sbp'%tag, err) 
     169    found = self.wait_and_validate(uid, result, msg, perf, '%s/spb'%tag) 
     170    if retry and not found: 
     171      return self.submit_payload(encryption, source, status, msg, perf, tag, False) 
     172    return result 
     173     
     174  def submit_via_exec(self, encryption, source, status, msg, perf, tag, retry=True): 
     175    uid = str(uuid.uuid4()) 
     176   
     177    args = [ 
     178      #'--exec', 'submit',  
     179      '--alias', uid,  
     180      '--result', '%d'%status,  
     181      '--message', '%s - %s'%(uid, msg),  
     182      '--encryption', encryption, 
     183      '--password', 'pwd-%s'%encryption, 
     184      '--host', '127.0.0.1:15667' 
     185      ] 
     186    (result_code, result_message) = core.simple_exec('any', 'nsca_submit', args) 
     187    result = TestResult() 
     188     
     189    result.add_message(result_code == 0, 'Testing to send message using %s/exec:1'%tag) 
     190    result.add_message(len(result_message) == 1, 'Testing to send message using %s/exec:2'%tag, len(result_message)) 
     191    result.add_message(len(result_message[0]) == 0, 'Testing to send message using %s/exec:3'%tag, result_message[0]) 
     192    found = self.wait_and_validate(uid, result, msg, perf, '%s/exec'%tag) 
     193    if retry and not found: 
     194      return self.submit_via_exec(encryption, source, status, msg, perf, tag, False) 
    167195    return result 
    168196 
    169197  def test_one_crypto_full(self, encryption, state, key): 
    170     r1 = self.submit_payload(encryption, '%ssrc%s'%(key, key), state, '%smsg%s'%(key, key), '') 
    171     if r1: 
    172       return r1 
    173     result = TestResult() 
    174     result.add_message(True, '*FAILED* to send message with %s (retrying)'%encryption) 
    175     r2 = self.submit_payload(encryption, '%ssrc%s'%(key, key), state, '%smsg%s'%(key, key), '') 
    176     if r2: 
    177       result.add(r2) 
    178       return result 
    179     result.add_message(False, 'Failed to send message with uuid: %s'%encryption) 
     198    result = TestResult() 
     199    result.add(self.submit_payload(encryption, '%ssrc%s'%(key, key), state, '%smsg%s'%(key, key), '', '%s/%s'%(state, encryption))) 
     200    result.add(self.submit_via_exec(encryption, '%ssrc%s'%(key, key), state, '%smsg%s'%(key, key), '', '%s/%s'%(state, encryption))) 
    180201    return result 
    181202 
     
    186207    core.reload('test_nsca_server') 
    187208    result = TestResult() 
     209    result.add_message(isOpen('localhost', 15667), 'Checking that port is open') 
    188210    result.add(self.test_one_crypto_full(crypto, status.UNKNOWN, 'unknown')) 
    189211    result.add(self.test_one_crypto_full(crypto, status.OK, 'ok')) 
     
    194216  def run_test(self): 
    195217    result = TestResult() 
    196     result.add_message(isOpen('localhost', 15667), 'Checking that port is open') 
    197218    # Currently broken: "xor" 
    198219    cryptos = ["des", "3des", "cast128", "xtea", "blowfish", "twofish", "rc2", "aes", "serpent", "gost", "none", "3way"] 
Note: See TracChangeset for help on using the changeset viewer.