Install Icinga2 and Icinga2-classicui with nginx on Debian8

1-Install Nginx web Server
Installing From NGINX Repository

   A-Download the key used to sign NGINX packages and repository
        to the apt program keyring and add it:
        #wget http://nginx.org/keys/nginx_signing.key
        #apt-key add nginx_signing.key
   B-Add the “sources” from which NGINX Open Source packages can be obtained: open the                       /etc/apt/sources.list file in any text editor, for example, vi:
        #vim /etc/apt/sources.list
   C-Add the lines to the file:
       Pre-Built Packages for Stable version
deb http://nginx.org/packages/debian/ jessie nginx #if debian7 =>change jessie to wheezy
deb-src http://nginx.org/packages/debian/ jessie nginx #if debian7 =>change jessie to wheezy
        Save and exit;
        #apt-get remove nginx-common
        #apt-get update
        #apt-get install nginx

         after completed install verify version:
         root@de8-nginx:~# nginx -v
         nginx version: nginx/1.10.0

2-Install Icinga2 and icinga2-classicui
    #apt-get install icinga2
    #apt-get install icinga2-classicui

    Configure icinga2-classicui with Nginx
    +For nginx/1.6.2
         go to create new file:
         #vim /etc/nginx/sites-available/icinga2-classicui
    +For nginx/1.10
         go to create this file:
         #vim /etc/nginx/conf.d/icinga2-classicui.conf
         Add this text:
server {
  listen 80;
  server_name  icinga2-classicui;
  root /usr/share/icinga2/classicui;
  index index.php index.html;

  access_log /var/log/nginx/icinga2-classicui-access.log;
  error_log /var/log/nginx/icinga2-classicui-error.log;


  auth_basic "Icinga Access";
  auth_basic_user_file  /etc/icinga2/classicui/htpasswd.users;
  location = / {
      rewrite ^/$ /icinga2-classicui/index.html permanent;
  }
  location /icinga2-classicui/stylesheets {
    alias   /etc/icinga/stylesheets;
  }
  location /icinga2-classicui {
    alias  /usr/share/icinga2/classicui;
  }
location ~ /cgi-bin/(.*)\.cgi$ {
    root /usr/lib/cgi-bin/icinga2-classicui;
    rewrite ^/cgi-bin/icinga2-classicui/(.*)\.cgi /$1.cgi break;
    include /etc/nginx/fastcgi_params;
    fastcgi_pass  unix:/var/run/fcgiwrap.socket;
    fastcgi_index index.php;
    fastcgi_param ICINGA_CGI_CONFIG /etc/icinga2/classicui/cgi.cfg;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    fastcgi_param  AUTH_USER          $remote_user;
    fastcgi_param  REMOTE_USER        $remote_user;
  }
# Security - Basic configuration
    location = /favicon.ico {
        log_not_found off;
        access_log off;
        expires max;
    }

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    # Deny access to hidden files
    location ~ /\. {
        deny all;
        access_log off;
        log_not_found off;
    }
   #setup redirect from /icinga2-classicui to /icinga2
        location /icinga2 {
               rewrite ^/* /icinga2-classicui last;
        }
}

Note:
listen 80; #default port 80 but we can change
server_name  icinga2-classicui;  #we to add in vim /etc/hosts file
Enable this site by symbol link:
#ln -s /etc/nginx/sites-available/icinga2-classicui  /etc/nginx/sites-enabled/
if work both of icinga2-classicui in different path is the same when any update
To disable icinga2-classicui we just delete on path /etc/nginx/sites-enabled/icinga2-classicui

Adding password for user icingaadmin. Here, I use password=password.
When Installation is finished then open your browser and type http://localhost/icinga2-classicui
or IP address of the installing machine. Ex.http://192.168.10.81/icinga2-classicui

verify icinga2 version go http://192.168.10.81/icinga2-classicui then check on backend:
Icinga Classic UI 1.11.6 (Backend r2.1.1-1)  =>on the top of icinga2-classicui
3-Icinga2 configuration

   3-A-Define Group Name for host
       I define Group Name Linux(server), huawei(network device) or other       
       #vim /etc/icinga2/conf.d/xm9200/groups/linux.conf
         add this:
object HostGroup "XM_Servers" {
  display_name = "XM_Linux Servers"
  assign where host.vars.os == "XM"  #XM Group will define in host file
}
       #vim /etc/icinga2/conf.d/xm9200/groups/huawei.conf
          add this:
object HostGroup "huawei" {
  display_name = "Huawei"
  assign where host.vars.os == "huawei"
}
  3-B-Add Host-Host Check
   -Create New Directory in /etc/icinga2/conf.d
#mkdir /etc/icinga2/conf.d/xm9200
-then create 3 directory in xm9200 ( hosts ,services ,groups)
#mkdir /etc/icinga2/conf.d/xm9200/hosts
#mkdir /etc/icinga2/conf.d/xm9200/services
#mkdir /etc/icinga2/conf.d/xm9200/groups
-We should saperate linux host file and switch file config 
  #vim /etc/icinga2/conf.d/xm9200/hosts/linux-server/linux.conf  #check all linux server
  #vim /etc/icinga2/conf.d/xm9200/hosts/switch/hauwei.conf        #check all switch
            - Then add configure as below:
#vim /etc/icinga2/conf.d/xm9200/hosts/linux.conf (check ping on host name cn9021 and                       xm6213, or other host will define on this file)
object Host "cn9021" {
                 import "generic-host"
                 address = "10.105.9.21"
                 display_name="cn9021-Asterisk-Server"
                 vars.os = "XM" #XM is group name define in groups
                 vars.sla = "24x7"
                 
  /* Add icon_image on host, default path: /usr/share/nagios/htdocs/images/logos/base */
                 vars.icon_image = "base/ubuntu.png"
                 vars.icon_image_alt = "Ubuntu Linux"
                 vars.vrml_image = "ubuntu.png"
                 vars.statusmap_image = "base/ubuntu.gd2"
   }

object Host "xm6213" {
                   import "generic-host"
                   address = "10.105.6.213"
                   display_name="xm6213"
                   vars.os = "XM"
                   vars.sla = "24x7"
                   
                   vars.icon_image = "base/ubuntu.png"
                   vars.icon_image_alt = "Ubuntu Linux"
                   vars.vrml_image = "ubuntu.png"
                   vars.statusmap_image = "base/ubuntu.gd2"
     }

   #vim /etc/icinga2/conf.d/xm9200/hosts/switch/hauwei.conf        
object Host "sw-hw-s5700-52c-ei-1001-svr" {
         import "generic-host"
         address = "10.105.1.1"
         display_name= "SW-HW-S5700-52C-EI-1001-SRV"
         vars.os = "huawei"
         vars.sla = "24x7"                                                                                                                                                           
}
object Host "sw-hw-s5700-1005-3rd" {
         import "generic-host"
         address = "10.105.1.5"
         display_name= "SW-HW-S5700-1005-3rd"
         vars.os = "huawei"
         vars.sla = "24x7"
 }
object Host "sw-hw-s5700-1006-3rd" {
         import "generic-host"
         address = "10.105.1.6"
         display_name= "SW-HW-S5700-1006-3rd"
         vars.os = "huawei"
         vars.sla = "24x7"
 }
   3-C-Check Other Services 
       3-C-1-Check Host Services (http, ssh, smtp,..)             
            #vim /etc/icinga2/conf.d/xm9200/services/xm9199.conf    #xm9199.conf is a hostname
            add this:
object Service "cpu_load" {
    import "generic-service"
    host_name = "xm9199"
    check_command = "load"
    vars.sla = "24x7"
}
object Service "cpu" {
    import "generic-service"
    host_name = "xm9199"
    check_command = "procs"
    vars.sla = "24x7"
}
object Service "users" {
    import "generic-service"
    host_name = "xm9199"
    check_command = "users"
    vars.sla = "24x7"
}
object Service "disk" {
    import "generic-service"
    host_name = "xm9199"
    check_command = "disk"
    vars.sla = "24x7"
}
object Service "ssh" {
    import "generic-service"
    host_name = "xm9199"
    check_command = "ssh"
    vars.sla = "24x7"
object Service "http" {
    import "generic-service"
    host_name = "xm9199"
    check_command = "http"
    vars.sla = "24x7"
}
Note:
check_command ="..." define in /usr/share/icinga2/include/command-plugins.conf
        3-C-2-Huawei switch check Port Services
            +install snmp-mibs-downloader
               Add this line to repository
               #vim /etc/apt/source.list
               Appen this:
 deb http://ftp.de.debian.org/debian jessie main non-free
              save and exit.
              #apt-get update
              #apt-get install snmp-mibs-downloader
            
              +Check Port Service using snmp so we need to configure snmp v2 on switch or network                       device
              after complete setup snmp on network device we will configure as below:
               +Download script check snmp on this:  download check_cisco.pl
                  Copy check_cisco.pl to   /usr/lib/nagios/plugins/
                  #chmod +x /usr/lib/nagios/plugins/check_cisco.pl
               +then we will define command to check with this plugin, add the end of file
                  #vim /usr/share/icinga2/include/command-plugins.conf
object CheckCommand "snmp_interface" {
        import "plugin-check-command"
        command = [ PluginDir + "/check_cisco.pl" ]
        timeout = 200
        arguments = {
                "-H" = "$snmp_address$"
                "-C" = "$snmp_community$"
                "-I" = "$check_interface$"
                }
        vars.snmp_address = "$address$"
        vars.snmp_community = "snmporpwd2016" 
        vars.check_interface = "$interface$"
}
Note:
vars.snmp_community = "snmporpwd2016"  is snmp name on network device 
Now we continue configure service check port by create file below:
#vim /etc/icinga2/conf.d/xm9200/services/huawei/sw-hw-s5700-1005-3rd.conf  #file name is the same hostname in host file
object Service "Port01" {
  import "generic-service"
  host_name = "sw-hw-s5700-1005-3rd"
  check_command = "snmp_interface" 
  vars.interface = "GigabitEthernet0/0/1"
  vars.sla = "24x7"
}
[-------------More Port--------------------------]                                                                                                                                                                               [------------------------------------------------]
}
object Service "Port24" {
  import "generic-service"
  host_name = "sw-hw-s5700-1005-3rd"
  check_command = "snmp_interface"
  vars.interface = "GigabitEthernet0/0/3"
  vars.sla = "24x7"
}
Note:

check_command = "snmp_interface" #manual define in file                  
3-C-3-Check APC UPS 
we need to download apc ups script: check_involtage.pl,  check_apc that have my gdrive icinga2:
Than copy to /usr/lib/nagios/plugins/
#chmod +x  /usr/lib/nagios/plugins/check_involtage.pl
#chmod +x  /usr/lib/nagios/plugins/check_apc

       +Defind command icinga2 file 
          #vim /usr/share/icinga2/include/command-plugins.conf
          Append this and change opennmsropwd to your snmp
object CheckCommand "apc_involtage" {
 import "plugin-check-command"
 command = PluginDir + "/check_involtage.pl  -H $host.address$ -p opennmsropwd "
}
object CheckCommand "apc_batcharge" {
 import "plugin-check-command"
 command = PluginDir + "/check_apc -H $host.address$ bcharge"
}
object CheckCommand "apc_batcap" {
 import "plugin-check-command"
 command = PluginDir + "/check_snmp -H $host.address$ -C opennmsropwd -o .1.3.6.1.4.1.318.1.1.1.2.2.1.0  -l 'Adv. Battery Capacity ' -u '%' "
}
object CheckCommand "apc_temp" {
 import "plugin-check-command"
 command = PluginDir + "/check_snmp -H $host.address$ -C opennmsropwd -o .1.3.6.1.4.1.318.1.1.1.2.2.2.0 -w 25 -c 30 -l 'Internal Temperature' -u 'Grad Celsius' "
}
object CheckCommand "apc_load" {
 import "plugin-check-command"
 command = PluginDir + "/check_snmp -H $host.address$ -C opennmsropwd -o .1.3.6.1.4.1.318.1.1.1.4.2.3.0 -w 60 -c 75 -l 'Output Load' -u '%' "
}
object CheckCommand "apc_current" {
 import "plugin-check-command"
 command = PluginDir + "/check_snmp -H $host.address$ -C opennmsropwd -o .1.3.6.1.4.1.318.1.1.1.4.2.4.0 -w 30 -c 50 -l 'Output Current' -u 'Ampere 3~' "
}
object CheckCommand "apc_frequency" {
 import "plugin-check-command"
 command = PluginDir + "/check_snmp -H $host.address$ -C opennmsropwd -o .1.3.6.1.4.1.318.1.1.1.4.2.2.0 -l 'Output Frequency' -u 'Herz' "
}

        Defind hostgroups
        #vim /etc/icinga2/conf.d/xm200/groups/ups.conf
object HostGroup "ups" {
  display_name = "UPS Power Monitor"
  assign where host.vars.os == "ups"
}
       defind hosts
        #vim /etc/icinga2/conf.d/xm200/hosts/ups.conf
object Host "ups-name" {
  import "generic-host"
  address = "10.214.0.4"
  vars.os = "ups"
  vars.sla = "24x7"
}
       defind services
        #vim /etc/icinga2/conf.d/xm200/services/ups.conf
object Service "Input Voltage" {
  import "generic-service"
  host_name = "PNHA13-UP-3KW01"
  check_command = "apc_involtage"
  vars.sla = "24x7"
}
 object Service "Battery Capacity" {
  import "generic-service"
  host_name = "PNHA13-UP-3KW01"
  check_command = "apc_batcap"
  vars.sla = "24x7"
}
 object Service "Output Load" {
  import "generic-service"
  host_name = "PNHA13-UP-3KW01"
  check_command = "apc_load"
  vars.sla = "24x7"
}
object Service "Output Current" {
  import "generic-service"
  host_name = "PNHA13-UP-3KW01"
  check_command = "apc_current"
  vars.sla = "24x7"
}
object Service "Output Frequency" {
  import "generic-service"
  host_name = "PNHA13-UP-3KW01"
  check_command = "apc_frequency"
  vars.sla = "24x7"
3-D-Email Notification

Setup using mail in Ubuntu you need mailutils and sSMTP. sSMTP just sends mails to a SMTP server you have access to.

#apt-get update && sudo apt-get upgrade -y
#apt-get install mailutils ssmtp -y

Configure sSMTP
Edit the /etc/ssmtp/ssmtp.conf. Uncomment FromLineOverride=YES and add your mailhub configuration. Here is a sample sSMTP config for a GMail SMTP:
# Config file for sSMTP sendmail
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
root=abc@xm.shv   #Email that will host send to

# The place where the mail goes. The actual machine name is required no 
# MX records are consulted. Commonly mailhosts are named mail.domain.com
mailhub=10.105.9.199

# Where will the mail seem to come from?
#rewriteDomain=
# The full hostname
hostname=xm9200-icinaga2
# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address
# NO - Use the system generated From: address
FromLineOverride=YES
Test your setup
#echo "Hello receiver" | mail -s "Test" someone@2342424422.com
setup with gmail smtp just add this
AuthUser=@gmail.com
AuthPass=Your-Gmail-Password
mailhub=smtp.gmail.com:587
UseSTARTTLS=YES
Configure on icinga2
Add email user will send to when host or service have any problem
#vim /etc/icinga2/conf.d/users.conf
object User "icingaadmin" {
  import "generic-user"
  display_name = "Icinga 2 Admin"
  groups = [ "icingaadmins" ]
  email = "samnang@xm.shv"
}
object UserGroup "icingaadmins" {
  display_name = "Icinga 2 Admin Group"
}
#vim /etc/icinga2/conf.d/notifications.conf
Verify and edit match similar as below:
apply Notification "mail-icingaadmin" to Host {
  import "mail-host-notification"
  user_groups = [ "icingaadmins" ]
  assign where host.vars.sla == "24x7"
}
apply Notification "mail-icingaadmin" to Service {
  import "mail-service-notification"
  user_groups = [ "icingaadmins" ]
  assign where service.vars.sla == "24x7"
}
Verify and appent as below:
#/etc/icinga2/conf.d/templates.conf
template User "generic-user" { } template Notification "mail-host-notification" { command = "mail-host-notification" states = [ Up, Down ] types = [ Problem, Acknowledgement, Recovery, Custom, FlappingStart, FlappingEnd, DowntimeStart, DowntimeEnd, DowntimeRemoved ] period = "24x7" } template Notification "mail-service-notification" { command = "mail-service-notification" states = [ OK, Warning, Critical, Unknown ] types = [ Problem, Acknowledgement, Recovery, Custom, FlappingStart, FlappingEnd, DowntimeStart, DowntimeEnd, DowntimeRemoved ] period = "24x7" }
Verify icinga2 configuration by:
#icinga2 daemon -C

4-Trouble Shooting
Error:Log
Could not open CGI config file '/etc/icinga2-classicui/cgi.cfg' for reading!
=>solution:
change path /etc/icinga2-classicui/cgi.cfg to match with your configure file path like my path is fastcgi_param ICINGA_CGI_CONFIG /etc/icinga2/classicui/cgi.cfg;
=============
Error:Log
[2016-05-05 14:57:58 +0800] warning/PluginCheckTask: Check command for object 'sw-hw-s5700-1005-3rd!Port01_To_Lenovo_Server' (PID: 3194, arguments: '/usr/lib/nagios/plugins/check_cisco.pl' '-C' 'snmporpwd2016' '-H' '10.105.1.5' '-I' 'GigabitEthernet0/0/1') terminated with exit code 255, output: MIB search path: $HOME/.snmp/mibs:/usr/share/snmp/mibs:/usr/share/snmp/mibs/iana:/usr/share/snmp/mibs/ietf:/usr/share/mibs/site:/usr/share/snmp/mibs:/usr/share/mibs/iana:/usr/share/mibs/ietf:/usr/share/mibs/netsnmp:/usr/share/snmp/mibs/
Cannot find module (IF-MIB): At line 1 in (none)
IF-MIB::ifDescr: Unknown Object Identifier
MIB search path: $HOME/.snmp/mibs:/usr/share/snmp/mibs:/usr/share/snmp/mibs/iana:/usr/share/snmp/mibs/ietf:/usr/share/mibs/site:/usr/share/snmp/mibs:/usr/share/mibs/iana:/usr/share/mibs/ietf:/usr/share/mibs/netsnmp:/usr/share/snmp/mibs/
Cannot find module (IF-MIB): At line 1 in (none)
IF-MIB::ifName: Unknown Object Identifier
Interface not found : please check your syntax for this device
=>solution:
we need to install snmp-mibs-downloader

Add this line to repository
#vim /etc/apt/source.list
appen this:
deb http://ftp.de.debian.org/debian jessie main non-free

save and exit.

#apt-get update
#apt-get install snmp-mibs-downloader
========================================
http://www.modius-techblog.de/linux/icinga-2-notifications/
https://errorin10.wordpress.com/2015/10/28/101-for-email-notifications-in-icinga2/