{"id":4470,"date":"2019-10-19T18:55:31","date_gmt":"2019-10-19T15:55:31","guid":{"rendered":"https:\/\/kifarunix.com\/?p=4470"},"modified":"2024-03-12T22:12:13","modified_gmt":"2024-03-12T19:12:13","slug":"add-hosts-to-nagios-server-for-monitoring","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/add-hosts-to-nagios-server-for-monitoring\/","title":{"rendered":"Add Hosts to Nagios Server For Monitoring"},"content":{"rendered":"\n<p>In this guide, we are going to learn how to add hosts to Nagios Server for monitoring. Well, in order to monitor your remote hosts with Nagios server for availability and metric checks, you need to add them hosts to the Nagios server.<\/p>\n\n\n\n<p>Before you can proceed, ensure that your Nagios is up and running;<\/p>\n\n\n\n<p><a rel=\"noreferrer noopener\" href=\"https:\/\/kifarunix.com\/install-nagios-server-on-centos-8\/\" target=\"_blank\">Install Nagios Server on CentOS 8<\/a><\/p>\n\n\n\n<p><a rel=\"noreferrer noopener\" href=\"https:\/\/kifarunix.com\/install-nagios-core-on-debian-10-buster\/\" target=\"_blank\">Install Nagios Core on Debian 10 Buster<\/a><\/p>\n\n\n\n<p><a rel=\"noreferrer noopener\" href=\"https:\/\/kifarunix.com\/how-to-install-and-configure-nagios-core-from-the-source-ubuntu-18-04\/\" target=\"_blank\">How to Install and Configure Nagios Core From the Source Ubuntu 18.04<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Adding Hosts to Nagios Server For Monitoring<\/h2>\n\n\n\n<p>Once the Nagios server is up and running, ssh into it and add the hosts as follows.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Create Nagios Host Object Definition<\/h3>\n\n\n\n<p>To begin with, create Nagios host object definition. A host definition is used to define a physical server, workstation, device within a network.<\/p>\n\n\n\n<p>By default, Nagios creates a localhost definition file under the Objects configuration directory, <strong><code>\/usr\/local\/nagios\/etc\/objects<\/code><\/strong>, if at all you installed Nagios from the source. Other object definitions are stored in this directory too.<\/p>\n\n\n\n<p>Therefore, create a sub-directory to store your own objects.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>mkdir \/usr\/local\/nagios\/etc\/objects\/kifarunix-demo<\/code><\/pre>\n\n\n\n<p>If you are using a custom directory as above where you will define your own object definition configurations,<strong> you need to tell Nagios to process all config files in that directory<\/strong>.<\/p>\n\n\n\n<p>Open main Nagios configuration file and insert the line, <code>cfg_dir=\/usr\/local\/nagios\/etc\/objects\/kifarunix-demo<\/code> under the <strong>OBJECT CONFIGURATION FILE(S)<\/strong> section.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>vim \/usr\/local\/nagios\/etc\/nagios.cfg<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\n...\n# OBJECT CONFIGURATION FILE(S)\n# These are the object configuration files in which you define hosts,\n# host groups, contacts, contact groups, services, etc.\n# You can split your object definitions across several config files\n# if you wish (as shown below), or keep them all in a single config file.\n\n# You can specify individual object config files as shown below:\ncfg_file=\/usr\/local\/nagios\/etc\/objects\/commands.cfg\n...\n...\n<strong># Definitions for Monitoring Kifarunix-demo hosts\ncfg_dir=\/usr\/local\/nagios\/etc\/objects\/kifarunix-demo<\/strong>\n<\/code><\/pre>\n\n\n\n<p>Next, create your host object definition file.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>vim \/usr\/local\/nagios\/etc\/objects\/kifarunix-demo\/hosts.cfg<\/code><\/pre>\n\n\n\n<p>Next, define you hosts details such as hostname, IP address and the host template to use.<\/p>\n\n\n\n<pre class=\"scroll-box\"><code>\n###############################################################################\n#\n# REMOTE HOST DEFINITION\n#\n###############################################################################\n\ndefine host {\n    use                     <strong>kifarunix-demo-hosts<\/strong>\n    host_name               web01.kifarunix-demo.com\n    alias                   web01\n    address                 192.168.2.10\n}\n\ndefine host {\n    use                     <strong>kifarunix-demo-hosts<\/strong>\n    host_name               hrm.kifarunix-demo.com\n    alias                   hrm\n    address                 192.168.56.109\n}\n<\/code><\/pre>\n\n\n\n<p>The line, <code><strong>use kifarunix-demo-hosts<\/strong><\/code> specifies the host template to use by the defined hosts. This ensures that the configurations defined in the specified template applies to the defined host.<\/p>\n\n\n\n<p>As such we will have to create the host template. You can as well choose to use the default host templates defined in the <code><strong>\/usr\/local\/nagios\/etc\/objects\/templates.cfg<\/strong><\/code> configuration file.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Create Nagios Host Group Object Definition<\/h3>\n\n\n\n<p>A host group definition is used to group one or more hosts. It makes it easy to specify the common checks that applies to similar hosts.<\/p>\n\n\n\n<p>For example, we can group the above two hosts in a group, <code><strong>Kifarunix-demo-env01<\/strong><\/code> aliased as <strong>Linux Servers<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>vim \/usr\/local\/nagios\/etc\/objects\/kifarunix-demo\/hostgroups.cfg<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\n###############################################################################\n# \n# REMOTE HOST GROUP DEFINITION\n#\n###############################################################################\ndefine hostgroup {\n    hostgroup_name         Kifarunix-demo-env01\n    alias                  Linux Servers\n    members                web01.kifarunix-demo.com,hrm.kifarunix-demo.com\n}\n<\/code><\/pre>\n\n\n\n<p>You can simply use the same file for the hosts and host groups object definition.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>vim \/usr\/local\/nagios\/etc\/objects\/kifarunix-demo\/host-hostgroup.cfg<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\n###############################################################################\n#\n# REMOTE HOST DEFINITION\n#\n###############################################################################\n\ndefine host {\n    use                     <strong>kifarunix-demo-hosts<\/strong>\n    host_name               web01.kifarunix-demo.com\n    alias                   web01\n    address                 192.168.2.10\n}\n\ndefine host {\n    use                     <strong>kifarunix-demo-hosts<\/strong> \n    host_name               hrm.kifarunix-demo.com\n    alias                   hrm\n    address                 192.168.56.109\n}\n###############################################################################\n# \n# REMOTE HOST GROUP DEFINITION\n#\n###############################################################################\ndefine hostgroup {\n    hostgroup_name         Kifarunix-demo-env01\n    alias                  Linux Servers\n    members                web01.kifarunix-demo.com,hrm.kifarunix-demo.com\n}\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Create Host and Service Template Configuration<\/h4>\n\n\n\n<p>Create a custom host and service definition template as shown below.<\/p>\n\n\n\n<p>The configurations defined in this template will be applied on any host that is defined to use it, for example the above.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>vim \/usr\/local\/nagios\/etc\/objects\/kifarunix-demo\/<strong>hosts-service-template.cfg<\/strong><\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\n# Host Template Definition\ndefine host{\nname                         kifarunix-demo-hosts\nnotifications_enabled        1\nevent_handler_enabled        1\nflap_detection_enabled       1\nprocess_perf_data            1\nretain_status_information    1\nretain_nonstatus_information 1\n        check_command                check-host-alive\n        check_interval               5\n        max_check_attempts           2\n        notification_interval        0\n        notification_period          24x7\n        notification_options         d,u,r\n        contact_groups               kifarunix-demo-admins\nregister                     0\n}\n\n# Service Template definition\ndefine service{\nname                         kifarunix-demo-service\nactive_checks_enabled        1\npassive_checks_enabled       1\nparallelize_check            1\nobsess_over_service          1\ncheck_freshness              0\nnotifications_enabled        1\nevent_handler_enabled        1\nflap_detection_enabled       1\nprocess_perf_data            1\nretain_status_information    1\nretain_nonstatus_information 1\n        notification_interval        0\n        is_volatile                  0\n        check_period                 24x7\n        check_interval               5\n        retry_interval               1\n        max_check_attempts           2\n        notification_period          24x7\n        notification_options         w,u,c,r\n        contact_groups               kifarunix-demo-admins\nregister                     0\n}\n<\/code><\/pre>\n\n\n\n<p>Save and exit the config<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Create Nagios Contacts<\/h3>\n\n\n\n<p>Note that in the above service template, we defined our contact group as <code><strong>kifarunix-demo-admins<\/strong><\/code>. Hence, you need to create a contact group, otherwise you can use the default nagiosadmins group.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>vim \/usr\/local\/nagios\/etc\/objects\/kifarunix-demo\/kifarunix-demo-contacts.cfg<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\n# Define Your Contacts Here\ndefine contact {\n    contact_name            koromicha         ; Short name of user\n    use                     generic-contact   ; Inherit default values from generic-contact template (defined above)\n    alias                   Koromicha         ; Full name of user\n    email                   koromicha@kifarunix-demo.com ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******\n}\ndefine contact {\n    contact_name            kifarunixdemoadmin\n    use                     generic-contact\n    alias                   Kifarunix-demo-admin            \n    email                   k.admin@kifarunix-demo.com \n}\n# Create Contact Group\ndefine contactgroup {\n\n    contactgroup_name       kifarunix-demo-admins\n    alias                   Kifarunix-demo Administrators\n    members                 koromicha,kifarunixdemoadmin\n}\n<\/code><\/pre>\n\n\n\n<p>Note that we are using default contact template as defined on the templates.cfg config file.<\/p>\n\n\n\n<p>Save and exit the configuration.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Define Service Metric Checks for the Remote Hosts<\/h3>\n\n\n\n<p>Let say you want to monitor remote hosts metrics such as disk space, users, load average, running procs, then below is a sample service metric check configuration file;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>vim \/usr\/local\/nagios\/etc\/objects\/kifarunix-demo\/<strong>hosts-services.cfg<\/strong><\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\ndefine service {\n        use                     kifarunix-demo-service\n        hostgroup_name          Kifarunix-demo-env01\n        service_description     Logged in Users\n        check_command           check_metric!check_users\n}\ndefine service {\n        use                     kifarunix-demo-service\n        hostgroup_name          Kifarunix-demo-env01\n        service_description     Load Average\n        check_command           check_metric!check_load\n}\ndefine service {\n        use                     kifarunix-demo-service\n        hostgroup_name          Kifarunix-demo-env01\n        service_description     Disk Usage\n        check_command           check_metric!check_disk\n}\ndefine service {\n        use                     kifarunix-demo-service\n        hostgroup_name          Kifarunix-demo-env01\n        service_description     Running Zombie Processes\n        check_command           check_metric!check_zombie_procs\n}\ndefine service {\n        use                     kifarunix-demo-service\n        hostgroup_name          Kifarunix-demo-env01\n        service_description     Running Processes\n        check_command           check_metric!check_total_procs\n}\n<\/code><\/pre>\n\n\n\n<p>If you check the service definition above, the check command is defined as, below, for example.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>check_command           check_metric!check_total_procs<\/code><\/pre>\n\n\n\n<p>The <code><strong>check_total_procs<\/strong><\/code> is the command defined on the remote agent NRPE configuration file. For example;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>command&#91;<strong>check_total_procs<\/strong>]=\/usr\/local\/nagios\/libexec\/check_procs -w 150 -c 200<\/code><\/pre>\n\n\n\n<p>While the <strong><code>check_metric<\/code><\/strong> is the name of the command that defines how Nagios server will use <strong><code>check_nrpe<\/code><\/strong> plugin to query the specific metric on the remote host.<\/p>\n\n\n\n<p>For example, we have defined the command in <code><strong>\/usr\/local\/nagios\/etc\/objects\/kifarunix-demo\/<strong>commands<\/strong>.cfg<\/strong><\/code> file;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>vim \/usr\/local\/nagios\/etc\/objects\/kifarunix-demo\/commands.cfg<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>define command {\n    command_name    check_metric\n    command_line    \/usr\/lib\/nagios\/plugins\/check_nrpe -H $HOSTADDRESS$ -c $ARG1$\n}<\/code><\/pre>\n\n\n\n<p>Now, test this command on the terminal to ensure that metrics are fetched;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/usr\/lib\/nagios\/plugins\/check_nrpe -H 192.168.56.109 -c check_load<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>OK - load average: 0.05, 0.02, 0.01|load1=0.050;15.000;30.000;0; load5=0.020;10.000;25.000;0; load15=0.005;5.000;20.000;0;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Verify Nagios Configuration<\/h3>\n\n\n\n<p>It is a good idea to always run a Nagio configuration file syntax check whenever you make any changes. This ensures that you fix any would be errors before restarting Nagios.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\/usr\/local\/nagios\/bin\/nagios -v \/usr\/local\/nagios\/etc\/nagios.cfg<\/code><\/pre>\n\n\n\n<p>You should get such an output. (Snipped)<\/p>\n\n\n\n<pre class=\"scroll-box\"><code>\nNagios Core 4.4.5\nCopyright (c) 2009-present Nagios Core Development Team and Community Contributors\nCopyright (c) 1999-2009 Ethan Galstad\nLast Modified: 2019-08-20\nLicense: GPL\n\nWebsite: https:\/\/www.nagios.org\nReading configuration data...\n   Read main config file okay...\n   Read object config files okay...\n\nRunning pre-flight check on configuration data...\n...\n(Output cut)\n...\n\n<strong>Total Warnings: 0\nTotal Errors:   0\n\nThings look okay - No serious problems were detected during the pre-flight check<\/strong>\n<\/code><\/pre>\n\n\n\n<p>If there are no errors, restart Nagios service.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>systemctl restart nagios<\/code><\/pre>\n\n\n\n<p>To confirm that the hosts and host groups have been added, login to Nagios on the browser and click <strong>Hosts <\/strong>and <strong>Host Groups<\/strong> under the <strong>Current Status<\/strong> section on the left pane.<\/p>\n\n\n\n<div><a href=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/11\/add-hosts.png\" class=\"td-modal-image\"><figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1343\" height=\"365\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/11\/add-hosts.png\" alt=\"Add Hosts to Nagios Server For Monitoring\" class=\"wp-image-10970\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/11\/add-hosts.png?v=1637260647 1343w, https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/11\/add-hosts-768x209.png?v=1637260647 768w\" sizes=\"(max-width: 1343px) 100vw, 1343px\" \/><\/figure><\/a><\/div>\n\n\n\n<div><a href=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/11\/add-hostgroups.png\" class=\"td-modal-image\"><figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1154\" height=\"391\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/11\/add-hostgroups.png\" alt=\"Add Hosts to Nagios Server For Monitoring\" class=\"wp-image-10971\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/11\/add-hostgroups.png?v=1637260747 1154w, https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/11\/add-hostgroups-768x260.png?v=1637260747 768w\" sizes=\"(max-width: 1154px) 100vw, 1154px\" \/><\/figure><\/a><\/div>\n\n\n\n<p>Service Status;<\/p>\n\n\n\n<div><a href=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/11\/current-status-services.png\" class=\"td-modal-image\"><figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1901\" height=\"590\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/11\/current-status-services.png\" alt=\"\" class=\"wp-image-10976\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/11\/current-status-services.png?v=1637264445 1901w, https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/11\/current-status-services-768x238.png?v=1637264445 768w, https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/11\/current-status-services-1536x477.png?v=1637264445 1536w\" sizes=\"(max-width: 1901px) 100vw, 1901px\" \/><\/figure><\/a><\/div>\n\n\n\n<p>Well, there you go. You have successfully added hosts to Nagios Server for Monitoring.<\/p>\n\n\n\n<p>You can now proceed to monitor hosts services and metrics.<\/p>\n\n\n\n<p>You can as well install Nagios Agents on the remote host for metric and service monitoring.<\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/monitor-linux-hosts-using-nagios-check_by_ssh-plugin\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">Monitor Linux Hosts using Nagios check_by_ssh Plugin<\/a><\/p>\n\n\n\n<p><a rel=\"noreferrer noopener\" href=\"https:\/\/kifarunix.com\/monitoring\/how-to-install-nagios-nrpe-agent-on-rhel-centos-oracle-linux\/\" target=\"_blank\">Install Nagios NRPE Monitoring Agent on Linux Host From the Source<\/a><\/p>\n\n\n\n<p><a rel=\"noreferrer noopener\" href=\"https:\/\/kifarunix.com\/security\/siem\/alienvault\/how-to-install-nsclient-nagios-monitoring-agent-on-windows-system\/\" target=\"_blank\">Install Nagios NSClient++ Monitoring Agent on Windows System<\/a><\/p>\n\n\n\n<p>Some Documentations and more guides<\/p>\n\n\n\n<p><a href=\"https:\/\/assets.nagios.com\/downloads\/nagioscore\/docs\/nagioscore\/4\/en\/toc.html\" target=\"_blank\" rel=\"noreferrer noopener\">Nagios Core Documentation<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, we are going to learn how to add hosts to Nagios Server for monitoring. Well, in order to monitor your remote hosts<\/p>\n","protected":false},"author":1,"featured_media":10215,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[121,72,73],"tags":[1162,1160,4280,76,145,1161],"class_list":["post-4470","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-howtos","category-monitoring","category-nagios","tag-add-hosts-nagios-server","tag-add-nagios-hosts","tag-add-nagios-hosts-monitoring","tag-nagios-core","tag-nagios-monitoring","tag-nagios-server","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50","resize-featured-image"],"_links":{"self":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/4470"}],"collection":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/comments?post=4470"}],"version-history":[{"count":9,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/4470\/revisions"}],"predecessor-version":[{"id":21222,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/4470\/revisions\/21222"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media\/10215"}],"wp:attachment":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media?parent=4470"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=4470"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=4470"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}