Help in shrinking some code

64 views
Skip to first unread message

brad.v...@gmail.com

unread,
Aug 4, 2021, 5:17:35 AM8/4/21
to Salt-users

I'm trying to set a CPU quota for McAfee ENS for Linux (ENSL).  ENSL has two services:  mfeespd and mfetpd (or isecespd and isectpd for an earlier version).  One should be able to run 'systemctl show -p CPUQuota mfeespd.sevice' to get the current CPU quota setting, but that is not working.  So, I have to see if the /etc/systemd/system/mfeespd.service.d/50-CPUQuota.conf file exists and if it does grep the value from it.  This is repeated for the mfetpd service.  The only difference is the path is /etc/systemd/system/mfetpd.service.d/50-CPUQuota.conf.  I can't figure out how to use a variable to hold the critical difference in the path so that I can loop for the two services instead of repeating the same code but just changing the path.  What I have is:

{# Setting Memory and CPU quotas #}
{% set CPUQUOTA1 = 'true' %}
{% set CPUQUOTA2 = 'true' %}
{% set MEMQUOTA1 = 'true' %}
{% set MEMQUOTA2 = 'true' %}
{% set RELOAD = 'false' %}
{% if salt['file.file_exists']('/etc/ systemd/system/mfeespd.service.d/50-CPUQuota.conf') %}
{% set CPUQ1 = salt['cmd.shell']("grep CPUQuota /etc/ systemd/system/mfeespd.service.d/50-CPUQuota.conf | cut -d= -f2 | cut -d% -f1") %}
{% set CPUQUOTA1 = 'false' %}
{% else %}
{% set CPUQ1 =0 %}
{% set RELOAD = 'true' %}
{% endif %}
{% if salt['file.file_exists']('/etc/ systemd/system/mfetpd.service.d/50-CPUQuota.conf') %}
{% set CPUQ2 = salt['cmd.shell']("grep CPUQuota /etc/ systemd/system/mfetpd.service.d/50-CPUQuota.conf | cut -d= -f2 | cut -d% -f1") %}
{% set CPUQUOTA2 = 'false' %}
{% else %}
{% set CPUQ2 =0 %}
{% set RELOAD = 'true' %}
{% endif %}
{% if CPUQUOTA1 == 'true' %}

set_cpuquota_mfeespd:
  cmd.run:
    - name: systemctl set-property mfeespd.service CPUQuota=10%

{% endif %}
{% if CPUQUOTA2 == 'true' %}

set_cpuquota_mfetpd:
  cmd.run:
    - name: systemctl set-property mfetpd.service CPUQuota=10%

{% endif %}
{% if RELOAD == 'true' %}
 daemon_reload_mfeespd:
  cmd.run:
    - name: systemctl daemon-reload

mfeespd_restart:
  cmd.run:
    - name: systemctl restart mfeespd.service

mfetpd_restart:
  cmd.run:
    - name: systemctl restart mfetpd.service

{% endif %}

If anyone has some suggestions, I'd love to hear them. 

Thanks!

viq

unread,
Aug 5, 2021, 7:23:15 AM8/5/21
to salt-...@googlegroups.com
On Wed, 2021-08-04 at 02:17 -0700, brad.v...@gmail.com wrote:
>
> I'm trying to set a CPU quota for McAfee ENS for Linux (ENSL).  ENSL
> has two services:  mfeespd and mfetpd (or isecespd and isectpd for an
> earlier version).  One should be able to run 'systemctl show -p
> CPUQuota mfeespd.sevice' to get the current CPU quota setting, but
> that is not working.  So, I have to see if the
> /etc/systemd/system/mfeespd.service.d/50-CPUQuota.conf file exists
> and if it does grep the value from it.  This is repeated for the
> mfetpd service.  The only difference is the path is
> /etc/systemd/system/mfetpd.service.d/50-CPUQuota.conf.  I can't
> figure out how to use a variable to hold the critical difference in
> the path so that I can loop for the two services instead of repeating
> the same code but just changing the path.  What I have is:

[cut]

> If anyone has some suggestions, I'd love to hear them. 

Manage the files with contents you want in them, instead of trying to
guess, and running some one-off commands to maybe set them. It's much
better for repeatability, and for sanity.
--
viq

Reply all
Reply to author
Forward
0 new messages