Last active
August 29, 2015 14:07
-
-
Save gladiatr72/64d7a7a5b7549ee9a42a to your computer and use it in GitHub Desktop.
salt example - using states, pillars and jinja looping
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{# /srv/salt/generic/nagios/plugins/check_disk/files/map.jinja -#} | |
{% from "nagios/plugins/check_disk/files/map.jinja" import check_disk with context -%} | |
{% include ".hash_headers.jinja" -%} | |
{% for node in check_disk -%} | |
command[check_disk{{ check_disk[node]['canonical'] }}]=/usr/lib64/nagios/plugins/check_disk{{ check_disk[node]['args'] }} | |
{% endfor -%} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
salt 'app-01.dev*' grains.get filesystems | |
app-01.dev.saltlab: | |
---------- | |
/: | |
---------- | |
canonical: | |
_root | |
device: | |
/dev/mapper/vg_localhost_localdomainsystem-lv_root | |
fstype: | |
ext4 | |
mountpt: | |
/ | |
size: | |
---------- | |
B: | |
7092494336.0 | |
G: | |
6.61 | |
K: | |
6926264.00 | |
M: | |
6763.93 | |
T: | |
0.01 | |
blocks: | |
1731566 | |
/boot: | |
---------- | |
canonical: | |
_boot | |
device: | |
/dev/sda1 | |
fstype: | |
ext4 | |
mountpt: | |
/boot | |
size: | |
---------- | |
B: | |
507744256.0 | |
G: | |
0.47 | |
K: | |
495844.00 | |
M: | |
484.22 | |
T: | |
0.00 | |
blocks: | |
495844 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{# /srv/salt/generic/nagios/plugins/check_disk/files/map.jinja #} | |
{% set host = salt['grains.get']('host') %} | |
{% set fss = salt['grains.get']('filesystems') %} | |
{% set pdata = salt['pillar.get']('nagios') %} | |
{% set check_disk = {} %} | |
{% set sizes = {} %} | |
{% for fs in fss.keys() -%} | |
{% set sizes = fss[fs]['size'] -%} | |
{% if fs == '/' -%} | |
{% set node = "/root" -%} | |
{% else %} | |
{% set node = fs -%} | |
{% endif -%} | |
{% set node = node.replace('/', '_') -%} {# app-01.dev.caltesting.org => app-01_dev #} | |
{% set check_name = 'check_disk%s'|format(node) %} {# check_disk_(root|boot|usr|var|etc) #} | |
{# see: salt '*' pillar.get nagios(.check_disk_root) #} | |
{% if check_name in pdata %} | |
{% set warn = pdata[check_name]['warn'] %} | |
{% set crit = pdata[check_name]['crit'] %} | |
{% set units = pdata[check_name]['units'] %} | |
{% else %} | |
{% if sizes['G']|float > 50.0 %} | |
{% set warn = 2048 %} | |
{% set crit = 512 %} | |
{% set units = 'MB' %} | |
{% elif sizes['G']|float > 20.0 %} | |
{% set warn = 1280 %} | |
{% set crit = 512 %} | |
{% set units = 'MB' %} | |
{% elif sizes['G']|float > 5.0 %} | |
{% set warn = 512 %} | |
{% set crit = 256 %} | |
{% set units = 'MB' %} | |
{% else %} | |
{% set warn = 100 %} | |
{% set crit = 30 %} | |
{% set units = 'MB' %} | |
{% endif %} | |
{% endif %} | |
{# the plugin <sarcasm> intelligently </sarcasm> uses '%' to denote #} | |
{# percentage-based parameters but uses a seperate argument (--units/-u) #} | |
{# to designate storage-unit-based parameters #} | |
{% if units == 'percentage' %} | |
{% set units = "" %} | |
{% set value_suffix = '%' %} | |
{% else %} | |
{% set units = ' --units %s'|format(units) %} | |
{% set value_suffix = "" %} | |
{% endif %} | |
{% do check_disk.update( { | |
fs: { | |
'args': | |
" -c %s%s -w %s%s %s %s"| | |
format( | |
crit, | |
value_suffix, | |
warn, | |
value_suffix, | |
units, | |
fs), | |
'canonical': fss[fs]['canonical'],} } ) %} | |
{% endfor %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{# /srv/salt/generic/nagios/plugins/check_disk/init.sls #} | |
{% from "nagios/plugins/check_disk/files/map.jinja" import check_disk with context -%} | |
/etc/nrpe.d/check_disk.cfg: | |
file.managed: | |
- user: root | |
- group: root | |
- mode: 644 | |
- source: salt://nagios/plugins/check_disk/files/check_disk.cfg | |
- template: jinja | |
- require: | |
- pkg: nrpe | |
- watch_in: | |
- service: nrpe | |
{% for node in check_disk -%} | |
{% set label = "%s%s"|format("check_disk", check_disk[node]['canonical']) -%} | |
nagios_monitors_{{ label }}: | |
grains.list_present: | |
- name: nagios_monitors | |
- value: | |
{{ label }} | |
- require_in: | |
- file: /etc/nrpe.d/check_disk.cfg | |
- watch_in: | |
- service: nrpe | |
{% endfor -%} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{# /srv/pillar/generic/nagios/config.sls -#} | |
files: | |
plugins: | |
path: | |
/usr/lib64/nagios/plugins | |
default: | |
check_dns | |
check_total_procs | |
check_zombie_procs | |
check_users | |
check_load | |
check_ntp_time | |
check_memory_free | |
check_procs | |
check_disk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{# /srv/pillar/generic/nagios/plugins/init.sls -#} | |
{% set os = grains['os_family'] %} | |
{% set minion_domain = grains['domain'] %} | |
{% set ns_domains = { | |
'subdomain1': [ 'subdomain1.domain.com', 'subdomain1.domain.net' ], | |
'subdomain2': [ 'sudomain2.domain.com', 'subdomain2.domain.net' ] | |
} %} | |
check_dns: | |
plugin: | |
check_dns | |
args: | |
host: www.domain.com | |
simple: True | |
check_total_procs: | |
plugin: | |
check_procs | |
args: | |
warn: 350 | |
crit: 400 | |
simple: True | |
check_zombie_procs: | |
plugin: | |
check_procs | |
args: | |
warn: 5 | |
crit: 10 | |
state: Z | |
simple: True | |
check_users: | |
plugin: | |
check_users | |
args: | |
warn: 20 | |
crit: 35 | |
simple: True | |
check_load: | |
plugin: | |
check_load | |
args: | |
warn: 4,5,6 | |
crit: 6,7,8 | |
simple: True | |
check_ntp_time: | |
plugin: | |
check_ntp_time | |
args: | |
{% if minion_domain in ns_domains['subdomain1'] %} | |
host: [ IP address of nagios host in subdomain1 ] | |
{% else %} | |
host: [ IP address of local NTP server ] | |
{% endif %} | |
warn: 2 | |
crit: 15 | |
simple: True | |
check_memory_free: | |
plugin: | |
check_memory_free.pl | |
args: | |
warn: 6 | |
crit: 4 | |
simple: True | |
check_disk: | |
plugin: | |
check_disk | |
simple: False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{# /srv/pillar/generic/nagios/init.sls #} | |
{% set bits = {} -%} | |
{% set host = opts.id -%} | |
{% if grains.host_canonical -%} | |
{% do bits.update({ 'canonical': grains.host_canonical }) -%} | |
{% endif -%} | |
{% set domain = grains['domain'] -%} | |
{% set pdata = salt['pillar.get']('nagios') -%} | |
include: | |
- nagios.plugins: | |
key: nagios_defaults | |
{% if grains.host_canonical -%} | |
- nagios.hosts.{{ domain }}.{{ grains.host_canonical }}: | |
key: nagios | |
{% endif -%} | |
{% if 'nagios_server' in bits -%} | |
{% endif -%} | |
- nagios.config: | |
key: nagios.config |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment