Acho que o principal problema é que load_avg
de cmd
não é um número, mas uma string e você precisa convertê-lo. Usando load_avg|float
(ou mesmo load_avg|int
) como este funciona:
{% set load_avg = salt['cmd.shell']('uptime | sed "s/.*load average: //" | cut -d " " -f2 | cut -d . -f1') %}
{% set threshold = 1 %}
check_load_avg:
cmd.run:
{% if load_avg|float >= threshold %}
- name: echo 'Load average is HIGH. load_avg={{ load_avg }}, threshold={{ threshold }}'
{% else %}
- name: echo 'Load average is normal. load_avg={{ load_avg }}, threshold={{ threshold }}'
{% endif %}
Saída:
----------
ID: check_load_avg
Function: cmd.run
Name: echo 'Load average is HIGH. load_avg=1, threshold=1'
Result: True
Comment: Command "echo 'Load average is HIGH. load_avg=1, threshold=1'" run
Started: 10:19:42.238409
Duration: 9.731 ms
Changes:
----------
pid:
5976
retcode:
0
stderr:
stdout:
Load average is HIGH. load_avg=1, threshold=1
com threshold=5
:
----------
ID: check_load_avg
Function: cmd.run
Name: echo 'Load average is normal. load_avg=1, threshold=5'
Result: True
Comment: Command "echo 'Load average is normal. load_avg=1, threshold=5'" run
Started: 10:20:31.846864
Duration: 9.361 ms
Changes:
----------
pid:
6184
retcode:
0
stderr:
stdout:
Load average is normal. load_avg=1, threshold=5
EDITAR:
Você pode obter a média de carga usando diretamente o módulo de sal status.loadavg
:
# salt-call status.loadavg
local:
----------
1-min:
0.12
15-min:
0.31
5-min:
0.25
Em jinja:
{% set load_avg = salt['status.loadavg']()['1-min'] %}