From 048acedfff6cfa0123fe7cd345fb4e1a3219402f Mon Sep 17 00:00:00 2001 From: Naparuba Date: Tue, 14 Feb 2012 15:19:46 +0100 Subject: [PATCH] Fix : (reported by : H4wkmoon) STAR expand in hostgroup names was not looking as template property. --- shinken/objects/item.py | 4 +- test/etc/nagios_star_in_hostgroups.cfg | 119 ++++++++++++++++++ test/etc/star_in_hostgroups/commands.cfg | 30 +++++ test/etc/star_in_hostgroups/contacts.cfg | 18 +++ test/etc/star_in_hostgroups/hostgroups.cfg | 62 +++++++++ test/etc/star_in_hostgroups/hosts.cfg | 74 +++++++++++ test/etc/star_in_hostgroups/servicegroups.cfg | 61 +++++++++ test/etc/star_in_hostgroups/services.cfg | 44 +++++++ .../star_in_hostgroups/shinken-specific.cfg | 114 +++++++++++++++++ test/etc/star_in_hostgroups/timeperiods.cfg | 11 ++ test/jenkins/longtests.txt | 1 + test/jenkins/shorttests.txt | 1 + test/quick_tests.sh | 1 + test/test_star_in_hostgroups.py | 58 +++++++++ 14 files changed, 596 insertions(+), 2 deletions(-) create mode 100644 test/etc/nagios_star_in_hostgroups.cfg create mode 100644 test/etc/star_in_hostgroups/commands.cfg create mode 100644 test/etc/star_in_hostgroups/contacts.cfg create mode 100644 test/etc/star_in_hostgroups/hostgroups.cfg create mode 100644 test/etc/star_in_hostgroups/hosts.cfg create mode 100644 test/etc/star_in_hostgroups/servicegroups.cfg create mode 100644 test/etc/star_in_hostgroups/services.cfg create mode 100644 test/etc/star_in_hostgroups/shinken-specific.cfg create mode 100644 test/etc/star_in_hostgroups/timeperiods.cfg create mode 100755 test/test_star_in_hostgroups.py diff --git a/shinken/objects/item.py b/shinken/objects/item.py index cbfd374256..b9e2f13671 100644 --- a/shinken/objects/item.py +++ b/shinken/objects/item.py @@ -1012,7 +1012,7 @@ def evaluate_hostgroup_expression(self, expr, hosts, hostgroups, look_in='hostgr gn = gn.replace('-', HostGroup_Name_Parse_Ctx.minus_sign_in_name) gn = gn.replace('print', HostGroup_Name_Parse_Ctx.print_in_name) newgroupname2hostnames[gn] = val - + set_res = [] try: set_res = set(eval(str_setexpr, newgroupname2hostnames, {})) @@ -1336,7 +1336,7 @@ def get_all_host_names_set(hosts): return set( h.host_name for h in hosts.items.values() - if getattr(h, 'host_name', '') != '' + if getattr(h, 'host_name', '') != '' and not h.is_tpl() ) diff --git a/test/etc/nagios_star_in_hostgroups.cfg b/test/etc/nagios_star_in_hostgroups.cfg new file mode 100644 index 0000000000..37c7f028fc --- /dev/null +++ b/test/etc/nagios_star_in_hostgroups.cfg @@ -0,0 +1,119 @@ +accept_passive_host_checks=1 +accept_passive_service_checks=1 +additional_freshness_latency=15 +admin_email=shinken@localhost +admin_pager=shinken@localhost +auto_reschedule_checks=0 +auto_rescheduling_interval=30 +auto_rescheduling_window=180 +cached_host_check_horizon=15 +cached_service_check_horizon=15 +cfg_file=star_in_hostgroups/hosts.cfg +cfg_file=star_in_hostgroups/services.cfg +cfg_file=star_in_hostgroups/contacts.cfg +cfg_file=star_in_hostgroups/commands.cfg +cfg_file=star_in_hostgroups/timeperiods.cfg +cfg_file=star_in_hostgroups/hostgroups.cfg +cfg_file=star_in_hostgroups/servicegroups.cfg +cfg_file=star_in_hostgroups/shinken-specific.cfg +check_external_commands=1 +check_for_orphaned_hosts=1 +check_for_orphaned_services=1 +check_host_freshness=0 +check_result_path=var/spool/checkresults +check_result_reaper_frequency=10 +check_service_freshness=1 +command_check_interval=-1 +command_file=var/shinken.cmd +daemon_dumps_core=0 +date_format=iso8601 +debug_file=var/shinken.debug +debug_level=112 +debug_verbosity=1 +enable_embedded_perl=0 +enable_environment_macros=1 +enable_event_handlers=1 +enable_flap_detection=0 +enable_notifications=1 +enable_predictive_host_dependency_checks=1 +enable_predictive_service_dependency_checks=1 +event_broker_options=-1 +event_handler_timeout=30 +execute_host_checks=1 +execute_service_checks=1 +external_command_buffer_slots=4096 +high_host_flap_threshold=20 +high_service_flap_threshold=20 +host_check_timeout=30 +host_freshness_check_interval=60 +host_inter_check_delay_method=s +illegal_macro_output_chars=`~\$&|'"<> +illegal_object_name_chars=`~!\$%^&*|'"<>?,()= +interval_length=60 +lock_file=var/shinken.pid +log_archive_path=var/archives +log_event_handlers=1 +log_external_commands=1 +log_file=var/nagios.log +log_host_retries=1 +log_initial_states=0 +log_notifications=1 +log_passive_checks=1 +log_rotation_method=d +log_service_retries=1 +low_host_flap_threshold=5 +low_service_flap_threshold=5 +max_check_result_file_age=3600 +max_check_result_reaper_time=30 +max_concurrent_checks=0 +max_debug_file_size=1000000 +max_host_check_spread=30 +max_service_check_spread=30 +nagios_group=shinken +nagios_user=shinken +notification_timeout=30 +object_cache_file=var/objects.cache +obsess_over_hosts=0 +obsess_over_services=0 +ocsp_timeout=5 +#p1_file=/tmp/test_shinken/plugins/p1.pl +p1_file=/usr/local/nagios/bin/p1.pl +passive_host_checks_are_soft=0 +perfdata_timeout=5 +precached_object_file=var/objects.precache +process_performance_data=1 +resource_file=resource.cfg +retain_state_information=1 +retained_contact_host_attribute_mask=0 +retained_contact_service_attribute_mask=0 +retained_host_attribute_mask=0 +retained_process_host_attribute_mask=0 +retained_process_service_attribute_mask=0 +retained_service_attribute_mask=0 +retention_update_interval=60 +service_check_timeout=60 +service_freshness_check_interval=60 +service_inter_check_delay_method=s +service_interleave_factor=s +##shinken_group=shinken +##shinken_user=shinken +#nagios_group=nagios +#nagios_user=nagios +sleep_time=0.25 +soft_state_dependencies=0 +state_retention_file=var/retention.dat +status_file=var/status.dat +status_update_interval=5 +temp_file=tmp/shinken.tmp +temp_path=var/tmp +translate_passive_host_checks=0 +use_aggressive_host_checking=0 +use_embedded_perl_implicitly=0 +use_large_installation_tweaks=0 +use_regexp_matching=0 +use_retained_program_state=1 +use_retained_scheduling_info=1 +use_syslog=0 +use_true_regexp_matching=0 +enable_problem_impacts_states_change=1 +no_event_handlers_during_downtimes=1 \ No newline at end of file diff --git a/test/etc/star_in_hostgroups/commands.cfg b/test/etc/star_in_hostgroups/commands.cfg new file mode 100644 index 0000000000..76144927a8 --- /dev/null +++ b/test/etc/star_in_hostgroups/commands.cfg @@ -0,0 +1,30 @@ +define command{ + command_name check-host-alive + command_line $USER1$/test_hostcheck.pl --type=$ARG1$ --failchance=2% --previous-state=$HOSTSTATE$ --state-duration=$HOSTDURATIONSEC$ --hostname $HOSTNAME$ +} +define command{ + command_name check-host-alive-parent + command_line $USER1$/test_hostcheck.pl --type=$ARG1$ --failchance=2% --previous-state=$HOSTSTATE$ --state-duration=$HOSTDURATIONSEC$ --parent-state=$ARG2$ --hostname $HOSTNAME$ +} +define command{ + command_name notify-host + #command_line sleep 1 && /bin/true + command_line $USER1$/notifier.pl --hostname $HOSTNAME$ --notificationtype $NOTIFICATIONTYPE$ --hoststate $HOSTSTATE$ --hostoutput $HOSTOUTPUT$ --longdatetime $LONGDATETIME$ --hostattempt $HOSTATTEMPT$ --hoststatetype $HOSTSTATETYPE$ +} +define command{ + command_name notify-service + command_line $USER1$/notifier.pl --hostname $HOSTNAME$ --servicedesc $SERVICEDESC$ --notificationtype $NOTIFICATIONTYPE$ --servicestate $SERVICESTATE$ --serviceoutput $SERVICEOUTPUT$ --longdatetime $LONGDATETIME$ --serviceattempt $SERVICEATTEMPT$ --servicestatetype $SERVICESTATETYPE$ + #command_line sleep 1 && /bin/true +} +define command{ + command_name check_service + command_line $USER1$/test_servicecheck.pl --type=$ARG1$ --failchance=5% --previous-state=$SERVICESTATE$ --state-duration=$SERVICEDURATIONSEC$ --total-critical-on-host=$TOTALHOSTSERVICESCRITICAL$ --total-warning-on-host=$TOTALHOSTSERVICESWARNING$ --hostname $HOSTNAME$ --servicedesc $SERVICEDESC$ --custom $_SERVICECUSTNAME$ +} +define command{ + command_name eventhandler + command_line $USER1$/test_eventhandler.pl $SERVICESTATE$ $SERVICESTATETYPE$ $SERVICEATTEMPT$ +} +define command{ + command_name special_macro + command_line $USER1$/nothing $ARG1$ +} diff --git a/test/etc/star_in_hostgroups/contacts.cfg b/test/etc/star_in_hostgroups/contacts.cfg new file mode 100644 index 0000000000..0d6848e746 --- /dev/null +++ b/test/etc/star_in_hostgroups/contacts.cfg @@ -0,0 +1,18 @@ +define contactgroup{ + contactgroup_name test_contact + alias test_contacts_alias + members test_contact +} + +define contact{ + contact_name test_contact + alias test_contact_alias + service_notification_period 24x7 + host_notification_period 24x7 + service_notification_options w,u,c,r,f + host_notification_options d,u,r,f,s + service_notification_commands notify-service + host_notification_commands notify-host + email nobody@localhost + can_submit_commands 1 +} diff --git a/test/etc/star_in_hostgroups/hostgroups.cfg b/test/etc/star_in_hostgroups/hostgroups.cfg new file mode 100644 index 0000000000..d1855487f3 --- /dev/null +++ b/test/etc/star_in_hostgroups/hostgroups.cfg @@ -0,0 +1,62 @@ + +define hostgroup { + hostgroup_name router + alias All Router Hosts +} + +define hostgroup { + hostgroup_name hostgroup_01 + alias hostgroup_alias_01 +} + +define hostgroup { + hostgroup_name hostgroup_02 + alias hostgroup_alias_02 +} + +define hostgroup { + hostgroup_name hostgroup_03 + alias hostgroup_alias_03 +} + +define hostgroup { + hostgroup_name hostgroup_04 + alias hostgroup_alias_04 +} + +define hostgroup { + hostgroup_name hostgroup_05 + alias hostgroup_alias_05 +} + +define hostgroup { + hostgroup_name up + alias All Up Hosts +} + +define hostgroup { + hostgroup_name down + alias All Down Hosts +} + +define hostgroup { + hostgroup_name pending + alias All Pending Hosts +} + +define hostgroup { + hostgroup_name random + alias All Random Hosts +} + +define hostgroup { + hostgroup_name flap + alias All Flapping Hosts +} + +define hostgroup { + hostgroup_name allhosts + alias All Hosts + members test_router_0,test_host_0 +} + diff --git a/test/etc/star_in_hostgroups/hosts.cfg b/test/etc/star_in_hostgroups/hosts.cfg new file mode 100644 index 0000000000..1005536e7b --- /dev/null +++ b/test/etc/star_in_hostgroups/hosts.cfg @@ -0,0 +1,74 @@ +define host{ + check_interval 1 + check_period 24x7 + contact_groups test_contact + event_handler_enabled 1 + failure_prediction_enabled 1 + flap_detection_enabled 1 + max_check_attempts 3 + name generic-host + notification_interval 1 + notification_options d,u,r,f,s + notification_period 24x7 + notifications_enabled 1 + process_perf_data 1 + register 0 + retain_nonstatus_information 1 + retain_status_information 1 + retry_interval 1 + notes_url /nagios/wiki/doku.php/$HOSTNAME$ + action_url /nagios/pnp/index.php?host=$HOSTNAME$ +} + +define host{ + action_url http://search.cpan.org/dist/Monitoring-Generator-TestConfig/ + address 127.0.0.1 + alias flap_0 + check_command check-host-alive!flap + check_period 24x7 + host_name test_router_0 + hostgroups router + icon_image ../../docs/images/switch.png?host=$HOSTNAME$ + icon_image_alt icon alt string + notes just a notes string + notes_url http://search.cpan.org/dist/Monitoring-Generator-TestConfig/README + use generic-host +} + +define host{ + address 127.0.0.1 + alias up_0 + check_command check-host-alive-parent!up!$HOSTSTATE:test_router_0$ + event_handler eventhandler + check_period 24x7 + host_name test_host_0 + hostgroups hostgroup_01,up + parents test_router_0 + use generic-host + criticity 5 + _ostype gnulinux + _oslicense gpl +} + + + +define hostgroup { + hostgroup_name all + alias All servers + members * +} + +define host { + use generic-host + host_name desktop08 + address localhost + register 0 +} + + +define service { + use generic-service + check_command check_service!ok + service_description TEST + hostgroup_name all +} \ No newline at end of file diff --git a/test/etc/star_in_hostgroups/servicegroups.cfg b/test/etc/star_in_hostgroups/servicegroups.cfg new file mode 100644 index 0000000000..8357e3a582 --- /dev/null +++ b/test/etc/star_in_hostgroups/servicegroups.cfg @@ -0,0 +1,61 @@ + +define servicegroup { + servicegroup_name servicegroup_01 + alias servicegroup_alias_01 +} + +define servicegroup { + servicegroup_name servicegroup_02 + alias servicegroup_alias_02 + members test_host_0,test_ok_0 +} + +define servicegroup { + servicegroup_name servicegroup_03 + alias servicegroup_alias_03 +} + +define servicegroup { + servicegroup_name servicegroup_04 + alias servicegroup_alias_04 +} + +define servicegroup { + servicegroup_name servicegroup_05 + alias servicegroup_alias_05 +} + +define servicegroup { + servicegroup_name ok + alias All Ok Services +} + +define servicegroup { + servicegroup_name warning + alias All Warning Services +} + +define servicegroup { + servicegroup_name unknown + alias All Unknown Services +} + +define servicegroup { + servicegroup_name critical + alias All Critical Services +} + +define servicegroup { + servicegroup_name pending + alias All Pending Services +} + +define servicegroup { + servicegroup_name random + alias All Random Services +} + +define servicegroup { + servicegroup_name flap + alias All Flapping Services +} diff --git a/test/etc/star_in_hostgroups/services.cfg b/test/etc/star_in_hostgroups/services.cfg new file mode 100644 index 0000000000..e16682e074 --- /dev/null +++ b/test/etc/star_in_hostgroups/services.cfg @@ -0,0 +1,44 @@ +define service{ + active_checks_enabled 1 + check_freshness 0 + check_interval 1 + check_period 24x7 + contact_groups test_contact + event_handler_enabled 1 + failure_prediction_enabled 1 + flap_detection_enabled 1 + is_volatile 0 + max_check_attempts 2 + name generic-service + notification_interval 1 + notification_options w,u,c,r,f,s + notification_period 24x7 + notifications_enabled 1 + obsess_over_service 1 + parallelize_check 1 + passive_checks_enabled 1 + process_perf_data 1 + register 0 + retain_nonstatus_information 1 + retain_status_information 1 + retry_interval 1 +} + +define service{ + active_checks_enabled 1 + check_command check_service!ok + check_interval 1 + host_name test_host_0 + icon_image ../../docs/images/tip.gif?host=$HOSTNAME$&srv=$SERVICEDESC$ + icon_image_alt icon alt string + notes just a notes string + retry_interval 1 + service_description test_ok_0 + servicegroups servicegroup_01,ok + use generic-service + event_handler eventhandler + notes_url /nagios/wiki/doku.php/$HOSTNAME$/$SERVICEDESC$ + action_url /nagios/pnp/index.php?host=$HOSTNAME$&srv=$SERVICEDESC$ + _custname custvalue +} + diff --git a/test/etc/star_in_hostgroups/shinken-specific.cfg b/test/etc/star_in_hostgroups/shinken-specific.cfg new file mode 100644 index 0000000000..e8be20663e --- /dev/null +++ b/test/etc/star_in_hostgroups/shinken-specific.cfg @@ -0,0 +1,114 @@ +#The log managment for ALL daemons (all in one log, cool isn't it? ). +define module{ + module_name Simple-log + module_type simple_log + path tmp/nagios.log + archive_path tmp +} + + +#Status.dat and objects.cache export. For the old Nagios +#interface +define module{ + module_name Status-Dat + module_type status_dat + status_file /usr/local/shinken/var/status.data + object_cache_file /usr/local/shinken/var/objects.cache + status_update_interval 15 ; update status.dat every 15s +} + +##All other modules thtat can be called if you have installed +#the databses, or if you want to test something else :) + +#Here the NDO/MySQL module +#So you can use with NagVis or Centreon +define module{ + module_name ToNdodb_Mysql + module_type ndodb_mysql + database ndo ; database name + user root ; user of the database + password root ; must be changed + host localhost ; host to connect to + character_set utf8 ;optionnal, UTF8 is the default +} + + +#Here a NDO/Oracle module. For Icinga web connection +#Or for DBA that do not like MySQL +define module{ + module_name ToNdodb_Oracle + module_type ndodb_oracle + database XE ;database name (listener in fact) + user system ;user to connect + password password ;Yes I know I have to change my default password... + oracle_home /usr/lib/oracle/xe/app/oracle/product/10.2.0/server ;optional, but can be useful +} + + +#Here for Merlin/MySQL. For the cool Ninja connection +define module{ + module_name ToMerlindb_Mysql + module_type merlindb + backend mysql ;backend to use, here mysql databse + database merlin ;database name + user root ; ? .. yes, the user of the database... + password root ; wtf? you ask? + host localhost ; host of the database + character_set utf8 ;optionnal, UTF8 is the default +} + + +#Here the Merlin/Sqlite. No one use it for now :) +#You look at something : it's also the merlindb module, like the previous, +#it's the same code, it's just the backend parameter that change (and path). +define module{ + module_name ToMerlindb_Sqlite + module_type merlindb + backend sqlite ;like the mysql, but sqlite :) + database_path /usr/local/shinken/var/merlindb.sqlite ;path of the sqlite file +} + + +#Here the couchdb export. Maybe use one day... +#I should do a mangodb too one day... +#and casandra... +#and voldemort... +#and all other NoSQL database in fact :) +define module{ + module_name ToCouchdb + module_type couchdb + user root + password root + host localhost +} + + +#Export services perfdata to flat file. for centreon or +#perfparse +define module{ + module_name Service-Perfdata + module_type service_perfdata + path tmp/service-perfdata + mode a ;optionnal. Here append + template $LASTSERVICECHECK$\t$HOSTNAME$\t$SERVICEDESC$\t$SERVICEOUTPUT$\t$SERVICESTATE$\t$SERVICEPERFDATA$\n +} + + +#For hosts this time +#like the previous, but for hosts.... +define module{ + module_name Host-Perfdata + module_type host_perfdata + path tmp/host-perfdata + mode a ;optionna. Here append + template $LASTHOSTCHECK$\t$HOSTNAME$\t$HOSTOUTPUT$\t$HOSTSTATE$\t$HOSTPERFDATA$\n +} + + +#You know livestatus? Yes, there a Livestatus module for shinken too :) +define module{ + module_name Livestatus + module_type livestatus + host * ; * = listen on all configured ip addresses + port 50000 ; port to listen +} diff --git a/test/etc/star_in_hostgroups/timeperiods.cfg b/test/etc/star_in_hostgroups/timeperiods.cfg new file mode 100644 index 0000000000..bfdb9a832e --- /dev/null +++ b/test/etc/star_in_hostgroups/timeperiods.cfg @@ -0,0 +1,11 @@ +define timeperiod{ + timeperiod_name 24x7 + alias 24 Hours A Day, 7 Days A Week + sunday 00:00-24:00 + monday 00:00-24:00 + tuesday 00:00-24:00 + wednesday 00:00-24:00 + thursday 00:00-24:00 + friday 00:00-24:00 + saturday 00:00-24:00 +} diff --git a/test/jenkins/longtests.txt b/test/jenkins/longtests.txt index 558c9dfa41..58d64c07e7 100644 --- a/test/jenkins/longtests.txt +++ b/test/jenkins/longtests.txt @@ -93,6 +93,7 @@ test_timeperiods_state_logs.py test_groups_with_no_alias.py test_define_with_space.py test_objects_and_notifways.py +test_star_in_hostgroups.py test_parse_perfdata.py # takes long diff --git a/test/jenkins/shorttests.txt b/test/jenkins/shorttests.txt index fa11196359..8ec2c954cc 100644 --- a/test/jenkins/shorttests.txt +++ b/test/jenkins/shorttests.txt @@ -100,4 +100,5 @@ test_groups_with_no_alias.py test_timeperiods_state_logs.py test_define_with_space.py test_objects_and_notifways.py +test_star_in_hostgroups.py test_parse_perfdata.py diff --git a/test/quick_tests.sh b/test/quick_tests.sh index 236b448d18..bef079c6d1 100755 --- a/test/quick_tests.sh +++ b/test/quick_tests.sh @@ -138,6 +138,7 @@ launch_and_assert test_notif_too_much.py launch_and_assert test_timeperiods_state_logs.py launch_and_assert test_define_with_space.py launch_and_assert test_objects_and_notifways.py +launch_and_assert test_star_in_hostgroups.py launch_and_assert test_maintenance_period.py #Live status is a bit longer than the previous, so we put it at the end. diff --git a/test/test_star_in_hostgroups.py b/test/test_star_in_hostgroups.py new file mode 100755 index 0000000000..f37b36a464 --- /dev/null +++ b/test/test_star_in_hostgroups.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python2.6 +#Copyright (C) 2009-2010 : +# Gabes Jean, naparuba@gmail.com +# Gerhard Lausser, Gerhard.Lausser@consol.de +# +#This file is part of Shinken. +# +#Shinken is free software: you can redistribute it and/or modify +#it under the terms of the GNU Affero General Public License as published by +#the Free Software Foundation, either version 3 of the License, or +#(at your option) any later version. +# +#Shinken is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +#GNU Affero General Public License for more details. +# +#You should have received a copy of the GNU Affero General Public License +#along with Shinken. If not, see . + +# +# This file is used to test reading and processing of config files +# + +#It's ugly I know.... +from shinken_test import * + + +class TestStarInGroups(ShinkenTest): + #Uncomment this is you want to use a specific configuration + #for your test + def setUp(self): + self.setup_with_file('etc/nagios_star_in_hostgroups.cfg') + + + # If we reach a good start, we are ok :) + # the bug was that an * hostgroup expand get all host_name != '' + # without looking at register 0 or not + def test_star_in_groups(self): + # + # Config is not correct because of a wrong relative path + # in the main config file + # + print "Get the hosts and services" + now = time.time() + host = self.sched.hosts.find_by_name("test_host_0") + host.checks_in_progress = [] + host.act_depend_of = [] # ignore the router + router = self.sched.hosts.find_by_name("test_router_0") + router.checks_in_progress = [] + router.act_depend_of = [] # ignore the router + svc = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "TEST") + self.assert_(svc is not None) + + +if __name__ == '__main__': + unittest.main() +