├── .gitattributes ├── .gitignore ├── LICENSE ├── README.md ├── chap1-code └── chap1-os-services.txt ├── chap10-code ├── base-infra.yml ├── base-nagios.yml ├── base-nconf.yml ├── base.yml ├── group_vars │ ├── all_containers │ ├── hosts │ └── nagios-server ├── hosts ├── post-nagios-install.yml ├── post-nconf-install.yml └── roles │ ├── create-nagios-user │ ├── files │ │ └── nagios-key │ └── tasks │ │ └── main.yml │ ├── infra-plugin-config │ ├── files │ │ ├── check_http │ │ ├── check_port.pl │ │ ├── check_snmp_process.pl │ │ ├── show_users │ │ ├── utils.pm │ │ └── utils.sh │ └── tasks │ │ └── main.yml │ ├── install-nagios │ ├── files │ │ ├── id_dsa │ │ ├── id_dsa.pub │ │ └── nagios-ubuntu-logo.tar │ └── tasks │ │ └── main.yml │ ├── install-nconf │ ├── tasks │ │ └── main.yml │ └── templates │ │ └── create-nconf-db.sql │ ├── nagios-plugins │ ├── files │ │ ├── NagiosConfig.zip │ │ └── nagios-plugins.tar │ └── tasks │ │ └── main.yml │ ├── nagios-post-install │ ├── files │ │ ├── deploy_local.sh │ │ └── nagios.txt │ └── tasks │ │ └── main.yml │ ├── nconf-post-install │ └── tasks │ │ └── main.yml │ └── snmp-config │ ├── tasks │ └── main.yml │ └── templates │ └── snmpd.conf ├── chap2-code └── chap2-ansible.txt ├── chap3-code ├── base.yml ├── create-users-env │ ├── tasks │ │ └── main.yml │ └── vars │ │ └── main.yml ├── group_vars │ └── util_container └── hosts ├── chap4-code ├── adjust-quotas │ ├── tasks │ │ └── main.yml │ └── vars │ │ └── main.yml ├── group_vars │ └── util_container ├── hosts └── quota-update.yml ├── chap5-code ├── create-snapshot │ ├── tasks │ │ └── main.yml │ └── vars │ │ └── main.yml ├── group_vars │ └── util_container ├── hosts └── snapshot-tenant.yml ├── chap6-code ├── group_vars │ └── util_container ├── hosts ├── instance-migrate │ ├── tasks │ │ └── main.yml │ └── vars │ │ └── main.yml └── migrate.yml ├── chap7-code └── ansible-coreos │ ├── base.yml │ ├── hosts │ └── roles │ └── defunctzombie.coreos-bootstrap │ ├── .editorconfig │ ├── .travis.yml │ ├── LICENSE │ ├── README.md │ ├── files │ ├── bootstrap.sh │ ├── get-pip.py │ └── runner │ ├── meta │ ├── .galaxy_install_info │ └── main.yml │ ├── tasks │ └── main.yml │ └── tests │ └── test.yml ├── chap8-code ├── config-admin-region │ ├── tasks │ │ └── main.yml │ └── vars │ │ └── main.yml ├── config-admin.yml ├── configure-region-authentication.txt ├── group_vars │ └── util_container ├── hosts ├── register-endpoints.yml └── register-endpoints │ ├── tasks │ └── main.yml │ └── vars │ └── main.yml └── chap9-code └── cloud-inventory ├── group_vars ├── galera_container └── util_container ├── hosts ├── inventory.yml └── roles ├── cloud-inventory ├── files │ └── cloud_report.sql └── tasks │ └── main.yml ├── cloud-usage └── tasks │ └── main.yml ├── network-inventory ├── files │ └── network_report.sql └── tasks │ └── main.yml ├── project-inventory ├── files │ └── project_report.sql └── tasks │ └── main.yml ├── user-inventory ├── files │ └── user_report.sql └── tasks │ └── main.yml └── volume-inventory ├── files └── volume_report.sql └── tasks └── main.yml /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # Windows shortcuts 18 | *.lnk 19 | 20 | # ========================= 21 | # Operating System Files 22 | # ========================= 23 | 24 | # OSX 25 | # ========================= 26 | 27 | .DS_Store 28 | .AppleDouble 29 | .LSOverride 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear in the root of a volume 35 | .DocumentRevisions-V100 36 | .fseventsd 37 | .Spotlight-V100 38 | .TemporaryItems 39 | .Trashes 40 | .VolumeIcon.icns 41 | 42 | # Directories potentially created on remote AFP share 43 | .AppleDB 44 | .AppleDesktop 45 | Network Trash Folder 46 | Temporary Items 47 | .apdisk 48 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Packt 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | # OpenStack Administration with Ansible 2 5 | This is the code repository for [OpenStack Administration with Ansible 2](https://www.packtpub.com/virtualization-and-cloud/openstack-administration-ansible-2-second-edition?utm_source=github&utm_medium=repository&utm_content=9781787121638), published by Packt. It contains all the supporting project files necessary to work through the book from start to finish. 6 | ## Instructions and Navigations 7 | All of the code is organized into folders. Each folder starts with a number followed by the application name. For example, chap2-code 8 | 9 | The code will look like the following: 10 | 11 | - name: User password assignment 12 | debug: msg="User {{ item.0 }} was added to {{ item.2 }} project, with the assigned password of {{ item.1 }}" 13 | with_together: 14 | - userid 15 | - passwdss.stdout_lines 16 | - tenantid 17 | 18 | ### Software requirements: 19 | 20 | * Linux based OS, OS X and/or any of the BSD’s 21 | 22 | * openstack-ansible (OSA): https://github.com/openstack/openstack-ansible 23 | * Ansible 2.0 or better : http://docs.ansible.com/ansible/intro_installation.html 24 | * Ansible Container : https://docs.ansible.com/ansible-container/installation.html 25 | * Kargo : https://github.com/kubernetes-incubator/kargo 26 | * Nagios : https://assets.nagios.com/downloads/nagioscore/docs/nagioscore/4/en/quickstart.html 27 | * Nconf : http://www.nconf.org/dokuwiki/doku.php?id=nconf:help:documentation:start:installation 28 | 29 | ## Related Products: 30 | 31 | * [OpenStack Cloud Computing Cookbook - Third Edition]( https://www.packtpub.com/virtualization-and-cloud/openstack-cloud-computing-cookbook-third-edition?utm_source=github&utm_medium=repository&utm_content=9781782174783 ) 32 | 33 | * [OpenStack: Building a Cloud Environment]( https://www.packtpub.com/virtualization-and-cloud/openstack-building-cloud-environment?utm_source=github&utm_medium=repository&utm_content=9781787123182 ) 34 | 35 | * [Ansible 2 for Beginners [Video]]( https://www.packtpub.com/networking-and-servers/ansible-2-beginners-video?utm_source=github&utm_medium=repository&utm_content=9781786465719 ) 36 | 37 | ###Suggestions and Feedback 38 | [Click here] ( https://docs.google.com/forms/d/e/1FAIpQLSe5qwunkGf6PUvzPirPDtuy1Du5Rlzew23UBp2S-P3wB-GcwQ/viewform ) if you have any feedback or suggestions. 39 | ### Download a free PDF 40 | 41 | If you have already purchased a print or Kindle version of this book, you can get a DRM-free PDF version at no cost.
Simply click on the link to claim your free PDF.
42 |

https://packt.link/free-ebook/9781785884610

-------------------------------------------------------------------------------- /chap1-code/chap1-os-services.txt: -------------------------------------------------------------------------------- 1 | ## List OpenStack Services 2 | 3 | ### Via API 4 | $ curl -d @credentials.json –X POST -H "Content-Type: application/json" http://127.0.0.1:5000/v3/auth/tokens | python -mjson.tool 5 | 6 | #### Authorization string 7 | { "auth": { "identity": { "methods": [ "password" ], "password": { "user": { "name": "admin", "domain": { "id": "default" }, "password": "passwd" } } } } } 8 | 9 | #### HTTP Header Output 10 | HTTP/1.1 201 Created Date: Tue, 20 Sep 2016 21:20:02 GMT Server: Apache X-Subject-Token: gAAAAABX4agC32nymQSbku39x1QPooKDpU2T9oPYapF6ZeY4QSA9EOqQZ8PcKqMT2j5m9uvOtC9c8d9szObciFr06stGo19tNueHDfvHbgRLFmjTg2k8Scu1Q4esvjbwth8aQ-qMSe4NRTWmD642i6pDfk_AIIQCNA Vary: X-Auth-Token x-openstack-request-id: req-8de6fa59-8256-4a07-b040-c21c4aee6064 Content-Length: 283 Content-Type: application/json 11 | #### API Request 12 | $ curl -X GET http://127.0.0.1:35357/v3/services -H "Accept: application/json" -H "X-Auth-Token: 907ca229af164a09918a661ffa224747" | python -mjson.tool 13 | 14 | #### JSON Output 15 | { "links": { "next": null, "previous": null, "self": "http://example.com/identity/v3/services" }, "services": [ { "description": "Nova Compute Service", "enabled": true, "id": "1999c3a858c7408fb586817620695098", "links": { "... }, "name": "nova", "type": "compute" }, { "description": "Cinder Volume Service V2", "enabled": true, "id": "39216610e75547f1883037e11976fc0f", "links": { "... }, "name": "cinderv2", "type": "volumev2" }, ... 16 | ### Via CLI 17 | 18 | #### OpenRC v2.0 file example 19 | # To use an OpenStack cloud you need to authenticate against keystone. 20 | export OS_ENDPOINT_TYPE=internalURL 21 | export OS_USERNAME=admin 22 | export OS_TENANT_NAME=admin 23 | export OS_AUTH_URL=http://127.0.0.1:5000/v2.0 24 | 25 | # With Keystone you pass the keystone password. 26 | echo "Please enter your OpenStack Password: " 27 | read -sr OS_PASSWORD_INPUT 28 | export OS_PASSWORD=$OS_PASSWORD_INPUT 29 | 30 | 31 | #### OpenRC v3.0 file example 32 | # *NOTE*: Using the 3 *Identity API* does not necessarily mean any other # OpenStack API is version 3. For example, your cloud provider may implement # Image API v1.1, Block Storage API v2, and Compute API v2.0. OS_AUTH_URL is # only for the Identity API served through keystone. export OS_AUTH_URL=http://172.29.238.2:5000/v3 # With the addition of Keystone we have standardized on the term **project** # as the entity that owns the resources. export OS_PROJECT_ID=5408dd3366e943b694cae90a04d71c88 export OS_PROJECT_NAME="admin" export OS_USER_DOMAIN_NAME="Default" if [ -z "$OS_USER_DOMAIN_NAME" ]; then unset OS_USER_DOMAIN_NAME; fi # unset v2.0 items in case set unset OS_TENANT_ID unset OS_TENANT_NAME # In addition to the owning entity (tenant), OpenStack stores the entity # performing the action as the **user**. export OS_USERNAME="admin" # With Keystone you pass the keystone password. echo "Please enter your OpenStack Password: " read -sr OS_PASSWORD_INPUT export OS_PASSWORD=$OS_PASSWORD_INPUT # If your configuration has multiple regions, we set that information here. # OS_REGION_NAME is optional and only valid in certain environments. export OS_REGION_NAME="RegionOne" # Don't leave a blank variable, unset it if it was empty if [ -z "$OS_REGION_NAME" ]; then unset OS_REGION_NAME; fi 33 | 34 | 35 | #### CLI commands 36 | $ source openrc 37 | $ keystone service-list 38 | OR 39 | $ openstack service list 40 | 41 | -------------------------------------------------------------------------------- /chap10-code/base-infra.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # This playbook deploys components needed for the Infrastructure hosts. 3 | 4 | - hosts: hosts 5 | remote_user: root 6 | become: true 7 | roles: 8 | - create-nagios-user 9 | - infra-plugin-config -------------------------------------------------------------------------------- /chap10-code/base-nagios.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # This playbook deploys components needed for Nagios. 3 | 4 | - hosts: nagios-server 5 | remote_user: root 6 | become: true 7 | roles: 8 | - install-nagios 9 | - nagios-plugins -------------------------------------------------------------------------------- /chap10-code/base-nconf.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # This playbook deploys components needed for NConf. 3 | 4 | - hosts: nagios-server 5 | remote_user: root 6 | become: true 7 | roles: 8 | - install-nconf -------------------------------------------------------------------------------- /chap10-code/base.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # This playbook deploys components needed for Infrastructure hosts and containers. 3 | 4 | - hosts: all_containers 5 | remote_user: root 6 | become: true 7 | roles: 8 | - snmp-config 9 | 10 | - hosts: hosts 11 | remote_user: root 12 | become: true 13 | roles: 14 | - snmp-config -------------------------------------------------------------------------------- /chap10-code/group_vars/all_containers: -------------------------------------------------------------------------------- 1 | # Here are variables related to the install 2 | 3 | USER: nagios 4 | SNMP_COMMUNITY: osad 5 | SYS_LOCATION: SAT 6 | SYS_CONTACT: support@rackspace.com 7 | 8 | -------------------------------------------------------------------------------- /chap10-code/group_vars/hosts: -------------------------------------------------------------------------------- 1 | # Here are variables related to the install 2 | 3 | USER: nagios 4 | SNMP_COMMUNITY: osad 5 | SYS_LOCATION: SAT 6 | SYS_CONTACT: support@rackspace.com 7 | 8 | -------------------------------------------------------------------------------- /chap10-code/group_vars/nagios-server: -------------------------------------------------------------------------------- 1 | # Here are variables related to the install 2 | 3 | DB_NAME: NCONF_DB 4 | DB_USER: root 5 | DB_PASS: passwd -------------------------------------------------------------------------------- /chap10-code/hosts: -------------------------------------------------------------------------------- 1 | 2 | [nagios-server] 3 | 021579-nagios01 4 | -------------------------------------------------------------------------------- /chap10-code/post-nagios-install.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # This playbook deploys components needed for NConf. 3 | 4 | - hosts: nagios-server 5 | remote_user: root 6 | become: true 7 | roles: 8 | - nagios-post-install -------------------------------------------------------------------------------- /chap10-code/post-nconf-install.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # This playbook deploys components needed for NConf. 3 | 4 | - hosts: nagios-server 5 | remote_user: root 6 | become: true 7 | roles: 8 | - nconf-post-install -------------------------------------------------------------------------------- /chap10-code/roles/create-nagios-user/files/nagios-key: -------------------------------------------------------------------------------- 1 | ssh-dss AAAAB3NzaC1kc3MAAACBAJI8cQX6ujs5iXdJ/vsqjHZeZHyT11hc7TJ6p5sb1nd8v8vfQ2Z1azyOZ0Zraz5gk9QkiBhM2lhulQKhHdcjh9SRuVWaiL6Xn074FWPNqWBqI5kMNDu87oxrFFqjhl9xHCicQL2FeL3J2K64OqhX7reTmnXeU4EpiUvGBiCEU9HjAAAAFQCmu5AuLABeUiu0DBJVFHiDLXR+zQAAAIB79/1R1zQdG95ZSGNwiyHTeb2rBTvSP11hbRYFpCJ0NwmGLOIajrbJsgPcHwmroAwBo2HlRaOcjtDSyzHxaUpTXwck0HnzuffnbEH18WvWYxFQ9LIk+xbQybakADJMVncd/AKbS3g+x9bPpcUwJT2ir5CCfwwol5YTS5icl3bZCwAAAIEAi93ycGNBLVtMeafSGiCdiHrPToO4HGokpepHuIo5ryaE8G6B5r4NyrJCudGfVYIiyhrUDcgUyV2PA91m/HqmxczhyexyqTy0Edev6FEPoxR5dh02NLd+zHa7yFNaGvT1meFM42hCltcIW61/ofveuijGzwdU9cohosFKeMrRC3w= nagios@021579-deploy01 -------------------------------------------------------------------------------- /chap10-code/roles/create-nagios-user/tasks/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | - name: Create Nagios user 4 | user: name="{{ USER }}" comment="{{ USER }} User" 5 | ignore_errors: yes 6 | 7 | - name: Create user .ssh directory 8 | file: path=/home/"{{ USER }}"/.ssh state=directory 9 | 10 | - name: Copy authorized keys 11 | copy: src=nagios-key dest=/home/"{{ USER }}"/.ssh/authorized_keys mode=0644 12 | 13 | - name: Set home directory permissions 14 | file: path=/home/"{{ USER }}" owner="{{ USER }}" group="{{ USER }}" recurse=yes -------------------------------------------------------------------------------- /chap10-code/roles/infra-plugin-config/files/check_http: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/OpenStack-Administration-with-Ansible-2/b873eba0b58ab6db8274fb8b68a4424533c19a54/chap10-code/roles/infra-plugin-config/files/check_http -------------------------------------------------------------------------------- /chap10-code/roles/infra-plugin-config/files/check_port.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl -w 2 | #=============================================================================== 3 | # 4 | # FILE: check_port.pl 5 | # 6 | # USAGE: check_port.pl -p -h (-c -w -v) 7 | # 8 | # DESCRIPTION: tests to see if the port is responding and can display timing 9 | # 10 | # OPTIONS: --- 11 | # REQUIREMENTS: --- 12 | # BUGS: --- 13 | # NOTES: --- 14 | # AUTHOR: Tim Pretlove 15 | # VERSION: 1.3 16 | # CREATED: 04/12/09 13:57:23 17 | # REVISION: --- 18 | # LICENCE: GNU 19 | # 20 | # AUTHOR: Jim Sander jim.sander@jdsmedia.net 21 | # VERSION: 1.2 22 | # MODIFIED: 10-04-2014 16:00 23 | # BUGS: Socket::pack_sockaddr_in, length is 0 error for unresolvable hostnames 24 | # NOTES: Fixed; now exits with '3', status UNKNOWN, and 'host lookup failed' 25 | # 26 | # This program is free software: you can redistribute it and/or modify 27 | # it under the terms of the GNU General Public License as published by 28 | # the Free Software Foundation, either version 3 of the License, or 29 | # (at your option) any later version. 30 | # 31 | # This program is distributed in the hope that it will be useful, 32 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 33 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 34 | # GNU General Public License for more details. 35 | # 36 | # You should have received a copy of the GNU General Public License 37 | # along with this program. If not, see . 38 | # 39 | #=============================================================================== 40 | 41 | use strict; 42 | use warnings; 43 | use Socket; 44 | use Getopt::Long; 45 | use Time::HiRes qw(gettimeofday tv_interval); 46 | 47 | my ($crit, $warn, $timeout, $host, $portnum, $verbose); 48 | GetOptions( 49 | 'crtitical=s' => \$crit, 50 | 'warning=s' => \$warn, 51 | 'timeout=s' => \$timeout, 52 | 'host=s' => \$host, 53 | 'port=s' => \$portnum, 54 | 'verbose' => \$verbose) or HELP_MESSAGE(); 55 | 56 | sub testport { 57 | my ($host,$port,$protocol,$timeout) = @_; 58 | my $startsec; 59 | my $elapsed = 0; 60 | if (!defined $timeout) { $timeout = 10 } 61 | if (!defined $protocol) { $protocol = "tcp" } 62 | my $proto = getprotobyname($protocol); 63 | my $iaddr = inet_aton($host); 64 | if ( !defined $iaddr ){ return 3,$elapsed; } 65 | my $paddr = sockaddr_in($port, $iaddr); 66 | $startsec = [gettimeofday()]; 67 | socket(SOCKET, PF_INET, SOCK_STREAM, $proto) or die "socket: $!"; 68 | eval { 69 | local $SIG{ALRM} = sub { die "timeout" }; 70 | alarm($timeout); 71 | connect(SOCKET, $paddr) or error(); 72 | alarm(0); 73 | }; 74 | if ($@) { 75 | close SOCKET || die "close: $!"; 76 | $elapsed = tv_interval ($startsec, [gettimeofday]); 77 | return "1",$elapsed; 78 | } else { 79 | close SOCKET || die "close: $!"; 80 | $elapsed = tv_interval ($startsec, [gettimeofday]); 81 | return "0",$elapsed; 82 | } 83 | } 84 | sub HELP_MESSAGE { 85 | print "$0 -p -h (-c -w -v)\n"; 86 | print "\t -p # port number to examine\n"; 87 | print "\t -h # hostname or ip address to contact\n"; 88 | print "\t -c # the number of seconds to wait before a going critical\n"; 89 | print "\t -w # the number of seconds to wait before a flagging a warning\n"; 90 | print "\t -v # displays nagios performance information\n"; 91 | print "\te.g $0 -p 80 -h www.google.com -c 1.5 -w 1.0 -v\n"; 92 | exit(4); 93 | } 94 | 95 | sub printperf { 96 | my ($warning,$critical,$elapsed) = @_; 97 | if ((defined $warning) && (defined $critical)) { 98 | print "|rta=$elapsed" . "s;$warning;$critical;0;$critical"; 99 | } else { 100 | print "|rta=$elapsed" 101 | } 102 | } 103 | 104 | sub test { 105 | my ($critical,$warning,$host,$portnum,$timeout) = @_; 106 | my $proto = "tcp"; 107 | my ($rc,$elapsed) = testport($host,$portnum,$proto,$timeout); 108 | if ($rc == 0) { 109 | if (defined $critical) { 110 | if ($critical <= $elapsed) { 111 | return 2,$elapsed; 112 | } 113 | } 114 | if (defined $warning) { 115 | if ($warning <= $elapsed) { 116 | return 1,$elapsed; 117 | } 118 | } 119 | return $rc,$elapsed; 120 | } else { 121 | return 2,$elapsed; 122 | } 123 | } 124 | 125 | unless ((defined $portnum) && (defined $host)) { 126 | HELP_MESSAGE(); 127 | exit 1; 128 | } 129 | if ((defined $crit) && (defined $warn)) { 130 | if ($crit <= $warn) { 131 | print "Error: warning is greater than critical will never reach warning\n"; 132 | exit 4; 133 | } 134 | } 135 | 136 | my @mess = qw(OK WARNING CRITICAL UNKNOWN); 137 | my @mess2 = ("is responding","is slow responding","is not responding","host lookup failed"); 138 | my ($rc,$elapsed) = test($crit,$warn,$host,$portnum,$timeout); 139 | print "PORT $portnum $mess[$rc]: $host/$portnum $mess2[$rc]"; 140 | if (defined $verbose) { 141 | printperf($warn,$crit,$elapsed); 142 | } 143 | exit($rc); 144 | -------------------------------------------------------------------------------- /chap10-code/roles/infra-plugin-config/files/check_snmp_process.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl -w 2 | ############################## check_snmp_process ############## 3 | # Version : 1.4 4 | # Date : March 12 2007 5 | # Author : Patrick Proy (patrick at proy.org) 6 | # Help : http://nagios.manubulon.com 7 | # Licence : GPL - http://www.fsf.org/licenses/gpl.txt 8 | # Contrib : Makina Corpus 9 | # TODO : put $o_delta as an option 10 | # Contrib : 11 | ############################################################### 12 | # 13 | # help : ./check_snmp_process -h 14 | 15 | ############### BASE DIRECTORY FOR TEMP FILE ######## 16 | my $o_base_dir="/tmp/tmp_Nagios_proc."; 17 | my $file_history=200; # number of data to keep in files. 18 | my $delta_of_time_to_make_average=300; # 5minutes by default 19 | 20 | use strict; 21 | use Net::SNMP; 22 | use Getopt::Long; 23 | 24 | # Nagios specific 25 | 26 | use lib "/usr/local/nagios/libexec"; 27 | use utils qw(%ERRORS $TIMEOUT); 28 | #my $TIMEOUT = 5; 29 | #my %ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=>4); 30 | 31 | # SNMP Datas 32 | my $process_table= '1.3.6.1.2.1.25.4.2.1'; 33 | my $index_table = '1.3.6.1.2.1.25.4.2.1.1'; 34 | my $run_name_table = '1.3.6.1.2.1.25.4.2.1.2'; 35 | my $run_path_table = '1.3.6.1.2.1.25.4.2.1.4'; 36 | my $proc_mem_table = '1.3.6.1.2.1.25.5.1.1.2'; # Kbytes 37 | my $proc_cpu_table = '1.3.6.1.2.1.25.5.1.1.1'; # Centi sec of CPU 38 | my $proc_run_state = '1.3.6.1.2.1.25.4.2.1.7'; 39 | 40 | # Globals 41 | 42 | my $Version='1.4'; 43 | 44 | my $o_host = undef; # hostname 45 | my $o_community =undef; # community 46 | my $o_port = 161; # port 47 | my $o_version2 = undef; #use snmp v2c 48 | my $o_descr = undef; # description filter 49 | my $o_warn = 0; # warning limit 50 | my @o_warnL= undef; # warning limits (min,max) 51 | my $o_crit= 0; # critical limit 52 | my @o_critL= undef; # critical limits (min,max) 53 | my $o_help= undef; # wan't some help ? 54 | my $o_verb= undef; # verbose mode 55 | my $o_version= undef; # print version 56 | my $o_noreg= undef; # Do not use Regexp for name 57 | my $o_path= undef; # check path instead of name 58 | my $o_inverse= undef; # checks max instead of min number of process 59 | my $o_get_all= undef; # get all tables at once 60 | my $o_timeout= 5; # Default 5s Timeout 61 | # SNMP V3 specific 62 | my $o_login= undef; # snmp v3 login 63 | my $o_passwd= undef; # snmp v3 passwd 64 | my $v3protocols=undef; # V3 protocol list. 65 | my $o_authproto='md5'; # Auth protocol 66 | my $o_privproto='des'; # Priv protocol 67 | my $o_privpass= undef; # priv password 68 | # SNMP Message size parameter (Makina Corpus contrib) 69 | my $o_octetlength=undef; 70 | # Memory & CPU 71 | my $o_mem= undef; # checks memory (max) 72 | my @o_memL= undef; # warn and crit level for mem 73 | my $o_mem_avg= undef; # cheks memory average 74 | my $o_cpu= undef; # checks CPU usage 75 | my @o_cpuL= undef; # warn and crit level for cpu 76 | my $o_delta= $delta_of_time_to_make_average; # delta time for CPU check 77 | 78 | # functions 79 | 80 | sub p_version { print "check_snmp_process version : $Version\n"; } 81 | 82 | sub print_usage { 83 | print "Usage: $0 [-v] -H -C [-2] | (-l login -x passwd) [-p ] -n [-w [,] -c [,max_proc] ] [-m, -a -u, ] [-t ] [-o ] [-f ] [-r] [-V] [-g]\n"; 84 | } 85 | 86 | sub isnotnum { # Return true if arg is not a number 87 | my $num = shift; 88 | if ( $num =~ /^-?(\d+\.?\d*)|(^\.\d+)$/ ) { return 0 ;} 89 | return 1; 90 | } 91 | 92 | # Get the alarm signal (just in case snmp timout screws up) 93 | $SIG{'ALRM'} = sub { 94 | print ("ERROR: Alarm signal (Nagios time-out)\n"); 95 | exit $ERRORS{"UNKNOWN"}; 96 | }; 97 | 98 | sub read_file { 99 | # Input : File, items_number 100 | # Returns : array of value : [line][item] 101 | my ($traffic_file,$items_number)=@_; 102 | my ($ligne,$n_rows)=(undef,0); 103 | my (@last_values,@file_values,$i); 104 | open(FILE,"<".$traffic_file) || return (1,0,0); 105 | 106 | while($ligne = ) 107 | { 108 | chomp($ligne); 109 | @file_values = split(":",$ligne); 110 | #verb("@file_values"); 111 | if ($#file_values >= ($items_number-1)) { 112 | # check if there is enough data, else ignore line 113 | for ( $i=0 ; $i< $items_number ; $i++ ) {$last_values[$n_rows][$i]=$file_values[$i]; } 114 | $n_rows++; 115 | } 116 | } 117 | close FILE; 118 | if ($n_rows != 0) { 119 | return (0,$n_rows,@last_values); 120 | } else { 121 | return (1,0,0); 122 | } 123 | } 124 | 125 | sub write_file { 126 | # Input : file , rows, items, array of value : [line][item] 127 | # Returns : 0 / OK, 1 / error 128 | my ($file_out,$rows,$item,@file_values)=@_; 129 | my $start_line= ($rows > $file_history) ? $rows - $file_history : 0; 130 | if ( open(FILE2,">".$file_out) ) { 131 | for (my $i=$start_line;$i<$rows;$i++) { 132 | for (my $j=0;$j<$item;$j++) { 133 | print FILE2 $file_values[$i][$j]; 134 | if ($j != ($item -1)) { print FILE2 ":" }; 135 | } 136 | print FILE2 "\n"; 137 | } 138 | close FILE2; 139 | return 0; 140 | } else { 141 | return 1; 142 | } 143 | } 144 | 145 | sub help { 146 | print "\nSNMP Process Monitor for Nagios version ",$Version,"\n"; 147 | print "GPL licence, (c)2004-2006 Patrick Proy\n\n"; 148 | print_usage(); 149 | print <, 165 | : Authentication protocol (md5|sha : default md5) 166 | : Priv protocole (des|aes : default des) 167 | -p, --port=PORT 168 | SNMP port (Default 161) 169 | -n, --name=NAME 170 | Name of the process (regexp) 171 | No trailing slash ! 172 | -r, --noregexp 173 | Do not use regexp to match NAME in description OID 174 | -f, --fullpath 175 | Use full path name instead of process name 176 | (Windows doesn't provide full path name) 177 | -w, --warn=MIN[,MAX] 178 | Number of process that will cause a warning 179 | -1 for no warning, MAX must be >0. Ex : -w-1,50 180 | -c, --critical=MIN[,MAX] 181 | number of process that will cause an error ( 182 | -1 for no critical, MAX must be >0. Ex : -c-1,50 183 | Notes on warning and critical : 184 | with the following options : -w m1,x1 -c m2,x2 185 | you must have : m2 <= m1 < x1 <= x2 186 | you can omit x1 or x2 or both 187 | -m, --memory=WARN,CRIT 188 | checks memory usage (default max of all process) 189 | values are warning and critical values in Mb 190 | -a, --average 191 | makes an average of memory used by process instead of max 192 | -u, --cpu=WARN,CRIT 193 | checks cpu usage of all process 194 | values are warning and critical values in % of CPU usage 195 | if more than one CPU, value can be > 100% : 100%=1 CPU 196 | -g, --getall 197 | In some cases, it is necessary to get all data at once because 198 | process die very frequently. 199 | This option eats bandwidth an cpu (for remote host) at breakfast. 200 | -o, --octetlength=INTEGER 201 | max-size of the SNMP message, usefull in case of Too Long responses. 202 | Be carefull with network filters. Range 484 - 65535, default are 203 | usually 1472,1452,1460 or 1440. 204 | -t, --timeout=INTEGER 205 | timeout for SNMP in seconds (Default: 5) 206 | -V, --version 207 | prints version number 208 | Note : 209 | CPU usage is in % of one cpu, so maximum can be 100% * number of CPU 210 | example : 211 | Browse process list :