Commit 5e50fc27 authored by remy's avatar remy
Browse files

first commit sample: check_services

parent e660bf8e
# Check Services This formula checks services/daemon from a pillar (pillar:machines/hosts\*.sls).
A SaltStack formula to check a list of services from pillar data. Usage:
\ No newline at end of file
```bash
salt '*' state.sls check_services
```
Actually, I am using it with json output. Then I read the results with a specific php file every morning (cron).
`services.new.sls` was written to avoid old legacy style for `module.run` ( https://docs.saltproject.io/en/3000/ref/states/all/salt.states.module.html ) calls, and the custom module (`_modules/customservice.py`) [^1], but the new style, by including all services in only one `module.run` does not allow me to check each service (not a `dict` returned).
A cleaner way to do this would be by including all the logic into one pillar file, or with `file.managed` to the json written file + a dedicated salt mine.
[^1] Indeed, I had some synchronziation issues with custom modules on salt minions version > 2019. I needed to run `salt '*' saltutil.sync_all` or at least `salt '*' saltutil.sync_modules`.
include:
- .services
machines212:
ns1:
ip: 192.168.212.87
user: isi
ldap_client: false
machine_type: container
url_mgmt: https://192.168.212.208:8006
SaltHostname: ns1.domain.tld
isidnsnames:
- ns1
isemdnsnames:
- ns1.isi
services:
- ssh
- cron
- knot
RsyncShareName:
- /root
- /etc
- /var/spool/cron
- /var/lib/knot
- /var/backups
BackupFilesExclude:
- /etc/ssh
- /root/.ssh
nas-isi:
ip: 192.168.212.85
user: isi
ldap_client: false
machine_type: host
url_mgmt: https://192.168.212.228
SaltHostname: nas-isi.domain.tld
services:
- ssh
- nfs
- rpcbind
- cron
RsyncShareName:
- /root
- /etc
- /var/spool/cron
BackupFilesExclude:
- /etc/ssh
- /root/.ssh
# from https://github.com/saltstack-formulas/dhcpd-formula/blob/master/dhcpd/map.jinja
{% set preset_servicenames = salt['grains.filter_by']({
'RedHat': {
'apache': 'httpd',
'cron': 'crond',
'dhcp': 'dhcpd',
'dns': 'named',
'docker': 'docker',
'ftp': 'proftpd',
'ganglia-client': 'gmond',
'mail': 'postfix',
'mysql': 'mysqld',
'nginx': 'nginx',
'nfs': 'nfs',
'sshgpu': '',
'nvidiadocker': 'nvidia-docker',
'postgres': 'postgresql',
'sge': 'sgemaster',
'ssh': 'sshd',
},
'Debian': {
'apache': 'apache2',
'cron': 'cron',
'dhcp': 'isc-dhcp-server',
'dns': 'bind9',
'docker': 'docker',
'ftp': 'proftpd',
'ganglia-client': 'ganglia-monitor',
'mail': 'postfix',
'mysql': 'mysql',
'nginx': 'nginx',
'nfs': 'nfs-kernel-server',
'sshgpu': 'docker-ssh-gpu',
'nvidiadocker': 'nvidia-docker',
'postgres': 'postgresql',
'sge': 'sgemaster',
'ssh': 'ssh',
},
}, grain='os_family') %}
#}, merge=salt['pillar.get']('dhcpd:lookup')) %}
\ No newline at end of file
{% from "check_services/services.map.jinja" import jinja_services with context %}
{%- if grains['os_family'] == 'RedHat' %}
{% set preset_servicenames = {
'apache': "httpd",
'cron': "crond",
'dhcp': "dhcpd",
'dns': "named",
'ftp': "proftpd",
'mail': "postfix",
'mysql': "mysqld",
'nfs': "nfs",
'sge': "sgemaster." + grains['host'],
'ssh': "sshd" } %}
{%- elif grains['os_family'] == 'Debian' %}
{% set preset_servicenames = {
'apache': "apache2",
'cron': "cron",
'dhcp': "isc-dhcp-server",
'dns': "bind9",
'ftp': "proftpd",
'mail': "postfix",
'mysql': "mysql",
'nfs': "nfs-kernel-server",
'sge': "sgemaster." + grains['host'],
'ssh': "ssh" } %}
{%- endif %}
{%- set fulldict = {} %}
# mixing machines from subnet 192.168.212.0/24 and 192.168.197.0/24
{%- set hosts212 = salt['pillar.get']('machines212', {}) %}
#{#%- set hosts197 = salt['pillar.get']('machines197', {}) %#}
{%- do fulldict.update(hosts212) %}
#{#%- do fulldict.update(hosts197) %#}
{%- for host, hostinfo in fulldict.items() %}
{%- if 'services' in hostinfo %}
{%- if 'SaltHostname' in hostinfo %}
{%- if grains['id'] == hostinfo['SaltHostname'] %}
{%- for service in hostinfo['services'] %}
{%- if service == "web" %}
{%- set service = salt['mine.get'](hostinfo['SaltHostname'], 'mine_pkgweb')[hostinfo['SaltHostname']] %}
{%- endif %}
{%- if service in preset_servicenames %}
{%- if service == "sge" %}
{{ preset_servicenames[service] }}__{{ grains['id'] }}:
cmd.run:
- name: pgrep sge_qmaster
{%- else %}
{{ preset_servicenames[service] }}__{{ grains['id'] }}:
module.run:
{%- if 'module.run' in salt['config.get']('use_superseded', []) %}
- customservice.status:
- name: {{ preset_servicenames[service] }}
{%- else %}
- name: customservice.status
- m_name: {{ preset_servicenames[service] }}
{%- endif %}
{%- endif %}
{%- else %}
# service name is the one written in the pillar.s file.s
{{ service }}__{{ grains['id'] }}:
module.run:
{%- if 'module.run' in salt['config.get']('use_superseded', []) %}
- customservice.status:
- name: {{ service }}
{%- else %}
- name: customservice.status
- m_name: {{ service }}
{%- endif %}
{%- endif %}
{%- endfor %}
{%- endif %}
{%- endif %}
{%- endif %}
{%- endfor %}
{%- if 'roles' in grains %}
{%- set itsroles = salt['grains.get']('roles', []) %}
{%- if 'nas' in itsroles %}
check_nfs_on_nas:
module.run:
{%- if 'module.run' in salt['config.get']('use_superseded', []) %}
- customservice.status:
- name: {{ preset_servicenames['nfs'] }}
{%- else %}
- name: customservice.status
- m_name: {{ preset_servicenames['nfs'] }}
{%- endif %}
{%- elif 'tftp' in itsroles %}
check_tftp_on_nas:
module.run:
{%- if 'module.run' in salt['config.get']('use_superseded', []) %}
- customservice.status:
- name: tftpd-hpa
{%- else %}
- name: customservice.status
- m_name: tftpd-hpa
{%- endif %}
{%- elif 'proxmoxha' in itsroles %}
check_ha-lrm_on_px:
module.run:
{%- if 'module.run' in salt['config.get']('use_superseded', []) %}
- customservice.status:
- name: pve-ha-lrm
{%- else %}
- name: customservice.status
- m_name: pve-ha-lrm
{%- endif %}
check_ha-crm_on_px:
module.run:
{%- if 'module.run' in salt['config.get']('use_superseded', []) %}
- customservice.status:
- name: pve-ha-crm
{%- else %}
- name: customservice.status
- m_name: pve-ha-crm
{%- endif %}
check_corosync_on_px:
module.run:
{%- if 'module.run' in salt['config.get']('use_superseded', []) %}
- customservice.status:
- name: corosync
{%- else %}
- name: customservice.status
- m_name: corosync
{%- endif %}
{%- endif %}
{%- endif %}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment