From: Michael Friedrich Date: Sat, 11 Oct 2014 18:38:24 +0000 (+0200) Subject: GraphiteWriter: Add support for customized metric prefix names X-Git-Tag: v2.2.0~409 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=57d945347fadf7991e921a21774fe67eb8fc20e3;p=icinga2 GraphiteWriter: Add support for customized metric prefix names Allows to use runtime macros and global constants for setting a custom metric prefix: const GraphiteEnv = "icinga.env1" object GraphiteWriter "graphite" { host_name_template = GraphiteEnv + ".$host.name$" service_name_template = GraphiteEnv + ".$host.name$.$service.name$" } fixes #7334 --- diff --git a/components/perfdata/graphitewriter.cpp b/components/perfdata/graphitewriter.cpp index c0d6d3536..9e9dd8fe6 100644 --- a/components/perfdata/graphitewriter.cpp +++ b/components/perfdata/graphitewriter.cpp @@ -111,14 +111,27 @@ void GraphiteWriter::CheckResultHandler(const Checkable::Ptr& checkable, const C String prefix; + MacroProcessor::ResolverList resolvers; + if (service) + resolvers.push_back(std::make_pair("service", service)); + resolvers.push_back(std::make_pair("host", host)); + resolvers.push_back(std::make_pair("icinga", IcingaApplication::GetInstance())); + if (service) { String serviceName = service->GetShortName(); SanitizeMetric(serviceName); - prefix = "icinga." + hostName + "." + serviceName; + + /* custom prefix or default pattern */ + prefix = MacroProcessor::ResolveMacros(GetServiceNameTemplate(), resolvers, cr); + if (prefix.IsEmpty()) + prefix = "icinga." + hostName + "." + serviceName; SendMetric(prefix, "state", service->GetState()); } else { - prefix = "icinga." + hostName; + /* custom prefix or default pattern */ + prefix = MacroProcessor::ResolveMacros(GetHostNameTemplate(), resolvers, cr); + if (prefix.IsEmpty()) + prefix = "icinga." + hostName; SendMetric(prefix, "state", host->GetState()); } diff --git a/components/perfdata/graphitewriter.ti b/components/perfdata/graphitewriter.ti index 29311b44c..b34a8eb94 100644 --- a/components/perfdata/graphitewriter.ti +++ b/components/perfdata/graphitewriter.ti @@ -11,6 +11,12 @@ class GraphiteWriter : DynamicObject [config] String port { default {{{ return "2003"; }}} }; + [config] String host_name_template { + default {{{ return "icinga.$host.name$"; }}} + }; + [config] String service_name_template { + default {{{ return "icinga.$host.name$.$service.name$"; }}} + }; }; } diff --git a/components/perfdata/perfdata-type.conf b/components/perfdata/perfdata-type.conf index 69ecd334a..011a881c1 100644 --- a/components/perfdata/perfdata-type.conf +++ b/components/perfdata/perfdata-type.conf @@ -30,4 +30,6 @@ %type GraphiteWriter { %attribute %string "host", %attribute %string "port", + %attribute %string "host_name_template", + %attribute %string "service_name_template" } diff --git a/doc/3-monitoring-basics.md b/doc/3-monitoring-basics.md index b904a1817..2fa840547 100644 --- a/doc/3-monitoring-basics.md +++ b/doc/3-monitoring-basics.md @@ -1836,6 +1836,20 @@ The current naming schema is icinga.. icinga... +You can customize the metric prefix name by using the `host_name_template` and +`service_name_template` configuration attributes. + +The example below uses [runtime macros](#runtime-macros) and a +[global constant](#global-constants) named `GraphiteEnv`. The constant name +is freely definable and should be put in the [constants.conf](#constants-conf) file. + + const GraphiteEnv = "icinga.env1" + + object GraphiteWriter "graphite" { + host_name_template = GraphiteEnv + ".$host.name$" + service_name_template = GraphiteEnv + ".$host.name$.$service.name$" + } + To make sure Icinga 2 writes a valid label into Graphite some characters are replaced with `_` in the target name: @@ -1854,6 +1868,7 @@ internal check statistic data to Graphite: current_attempt | current check attempt max_check_attempts | maximum check attempts until the hard state is reached reachable | checked object is reachable + downtime_depth | number of downtimes this object is in execution_time | check execution time latency | check latency state | current state of the checked object diff --git a/doc/6-configuring-icinga-2.md b/doc/6-configuring-icinga-2.md index 42b51112b..44ad75605 100644 --- a/doc/6-configuring-icinga-2.md +++ b/doc/6-configuring-icinga-2.md @@ -1300,10 +1300,21 @@ Example: Attributes: - Name |Description - ----------------|---------------- - host |**Optional.** Graphite Carbon host address. Defaults to '127.0.0.1'. - port |**Optional.** Graphite Carbon port. Defaults to 2003. + Name |Description + ----------------------|---------------------- + host |**Optional.** Graphite Carbon host address. Defaults to '127.0.0.1'. + port |**Optional.** Graphite Carbon port. Defaults to 2003. + host_name_template |**Optional.** Metric prefix for host name. Defaults to "icinga.$host.name$". + service_name_template |**Optional.** Metric prefix for service name. Defaults to "icinga.$host.name$.$service.name$". + +Metric prefix names can be modified using [runtime macros](#runtime-macros). + +Example with your custom [global constant](#global-constants) `GraphiteEnv`: + + const GraphiteEnv = "icinga.env1" + + host_name_template = GraphiteEnv + ".$host.name$" + service_name_template = GraphiteEnv + ".$host.name$.$service.name$" ### IdoMySqlConnection diff --git a/tools/syntax/nano/icinga2.nanorc b/tools/syntax/nano/icinga2.nanorc index bd3281dfc..c36710e75 100644 --- a/tools/syntax/nano/icinga2.nanorc +++ b/tools/syntax/nano/icinga2.nanorc @@ -26,12 +26,12 @@ icolor red "(^|^\s+)(comment|compat_log_path|crl_path|database|disable_checks| icolor red "(^|^\s+)(display_name|duration|email|enable_active_checks|enable_event_handler)" icolor red "(^|^\s+)(enable_flapping|enable_ha|enable_notifications|enable_passive_checks|enable_perfdata)" icolor red "(^|^\s+)(endpoints|env|event_command|failover_timeout|fixed|flapping_threshold|groups|host)" -icolor red "(^|^\s+)(host_format_template|host_name|host_perfdata_path|host_temp_path|icon_image)" +icolor red "(^|^\s+)(host_format_template|host_name|host_name_template|host_perfdata_path|host_temp_path|icon_image)" icolor red "(^|^\s+)(icon_image_alt|instance_description|instance_name|interval|key_path|log_dir)" icolor red "(^|^\s+)(log_duration|max_check_attempts|methods|name|notes|notes_url|objects_path)" icolor red "(^|^\s+)(pager|parent|parent_host_name|parent_service_name|password|path|period)" icolor red "(^|^\s+)(port|ranges|retry_interval|rotation_interval|rotation_method)" -icolor red "(^|^\s+)(service_format_template|service_name|service_perfdata_path|service_temp_path)" +icolor red "(^|^\s+)(service_format_template|service_name|service_name_template|service_perfdata_path|service_temp_path)" icolor red "(^|^\s+)(severity|socket_path|socket_type|spool_dir|states|status_path|table_prefix)" icolor red "(^|^\s+)(timeout|times|types|update_interval|user|user_groups|users|volatile)" icolor red "(^|^\s+)(vars\.\w+)" diff --git a/tools/syntax/vim/syntax/icinga2.vim b/tools/syntax/vim/syntax/icinga2.vim index 109d07513..3ea37488a 100644 --- a/tools/syntax/vim/syntax/icinga2.vim +++ b/tools/syntax/vim/syntax/icinga2.vim @@ -69,12 +69,12 @@ syn keyword icinga2ObjAttr contained comment compat_log_path crl_path database syn keyword icinga2ObjAttr contained display_name duration email enable_active_checks enable_event_handler syn keyword icinga2ObjAttr contained enable_flapping enable_ha enable_notifications enable_passive_checks enable_perfdata syn keyword icinga2ObjAttr contained endpoints env event_command failover_timeout fixed flapping_threshold groups host -syn keyword icinga2ObjAttr contained host_format_template host_name host_perfdata_path host_temp_path icon_image +syn keyword icinga2ObjAttr contained host_format_template host_name host_name_template host_perfdata_path host_temp_path icon_image syn keyword icinga2ObjAttr contained icon_image_alt instance_description instance_name interval key_path log_dir syn keyword icinga2ObjAttr contained log_duration max_check_attempts methods name notes notes_url objects_path syn keyword icinga2ObjAttr contained pager parent parent_host_name parent_service_name password path period syn keyword icinga2ObjAttr contained port ranges retry_interval rotation_interval rotation_method -syn keyword icinga2ObjAttr contained service_format_template service_name service_perfdata_path service_temp_path +syn keyword icinga2ObjAttr contained service_format_template service_name service_name_template service_perfdata_path service_temp_path syn keyword icinga2ObjAttr contained severity socket_path socket_type spool_dir states status_path table_prefix syn keyword icinga2ObjAttr contained timeout times types update_interval user user_groups users volatile syn match icinga2ObjAttr contained "\(vars.\w\+\)"