salt icon indicating copy to clipboard operation
salt copied to clipboard

Multiple Windows Server service beacon question

Open evenyougreg opened this issue 7 years ago • 8 comments

Description of Issue/Question

My employer, UW Health, wants a lot of the default OOB Windows Server services that are running/automatic to be kept that way at all times. What would be the best practice method to achieve this?

Right now we have about 4 or 5 application services being managed each with their own reactor.conf entry and each with their own /srv/reactor/*.sls file, but we want to add many more default running/automatic services to this managed list. There are about 25 services that we need to always be running.

Is there are more economical and programmatic approach than having 1 unique beacon, 1 unique reactor.conf entry and 1 unique reactor sls file to turn a service back on? Is there a way to batch them together in some kind of general windows service pool?

Thanks!

Greg

evenyougreg avatar May 11 '19 18:05 evenyougreg

@LaughHammers Thanks for the report. If you're using the service beacon then you can include all the services that you wish to monitor in a single beacon configuration. The event that fires will then include which service's status has changed and you can restart that service in the reactor. Currently, I don't believe it can fire an event that includes multiple services but I could be wrong (@saltstack/team-core?). @twangboy @dwoz Any additional ideas or thoughts since this is Windows?

garethgreenaway avatar May 13 '19 20:05 garethgreenaway

Thanks for your help! I know how I would bunch all of the services into a single beacon configuration file, so I think I am good on that, you mentioned I can get the service to restart within a /srv/reactor sls file? Could you give me an example of what that might look like?

Thanks!

Greg

evenyougreg avatar May 14 '19 02:05 evenyougreg

@garethgreenaway after some digging I found a code snippet on reddit that worked. Is this what you were talking about?

{%- if data[data['service_name']]['running'] == False %}
start {{data['service_name']}}:
  local.service.start:
  - tgt: {{data['id']}}
  - args:
    - name: {{data['service_name']}}
{%- endif %}

Also, there isn't anything that I can find in the documentation that says what the reactor.conf file can do as far as targeting that isn't targeting all with an *. I need to be able to target what minion should subscribe to what beacons by os grain:

I've tried G@os:Windows but that doesn't work.

reactor:
  - 'salt/beacon/ ???? /service/*':
    - /srv/reactor/windows_service_reactor.sls
  - 'salt/beacon/ ???? /service/*':
    - /srv/reactor/linux_service_reactor.sls

Thanks in advance,

Greg

evenyougreg avatar May 14 '19 04:05 evenyougreg

@LaughHammers That snippet is exactly what I was thinking of. As far as the targeting, unfortunately the reactor files do not have access to grains since they are running on the master. What you could do in that case would be to have the reactor file call another state file which would be run on the minion itself. The state file would start or restart the service, and you could have it check the os grain to ensure it's only running on Windows.

garethgreenaway avatar May 14 '19 17:05 garethgreenaway

Ok, well I am shooting from the hip big time here...

This doesn't work. I am not a programmer but I feel like the logic is sound. Can you assist?

{% if data[data['service_name']]['running'] %}
  {% if grains['os'] == 'Windows' %}
  start_{{data['service_name']}}:
    local.service.start:
    - tgt: {{data['id']}}
    - args:
      - name: {{data['service_name']}}
  {% endif %}
{% endif %}

evenyougreg avatar May 14 '19 18:05 evenyougreg

@LaughHammers No worries. Something like this that I used in a recent conference presentation.

reactor:

{% set service_name = data['service_name'] %}
start {{ service_name }}:
  local.state.apply:
  - tgt: {{data['id']}}
  - args:
    - name: restart_service
    - pillar:
        server_name: {{ data['id'] }}
        service_name: {{ service_name }}
        service_running: {{ data[service_name][‘running’] }}

The above reactor file will pull the "service_name" out of the data that is sent along from the beacon, it will then call the restart_service state file passing along server_name, service_name, and service_running as pillar values.

state file called from the reactor:

{% set server_name = salt['pillar.get']('server_name’) %}
{% set service_name = salt['pillar.get']('service_name’) %}
{% set service_running = salt[‘pillar.get’](‘service_running’) %)

{% if not service_running %}
{{ service_name }}:
  service.running:
{% endif %}

The state file will pull those values out of pillar include local scope variables. If the value of service_running is False, then a call to the service.running state function will force that service to start. Should be easy to adapt to your purposes to only handle services on Windows machines.

garethgreenaway avatar May 14 '19 19:05 garethgreenaway

Hi again @garethgreenaway, I am making progress I think! This is where I am at:

{% set minion = data['id'] %}
{% set service = data['service_name'] %}
{% set running = data[data['service_name']]['running'] %}

{% if data[data['service_name']]['running'] == False %}
start_{{ service }}:
  local.state.apply:
  - tgt: {{ minion }}
  - args:
    - mods: check_service
    - pillar:
        minion: {{ minion }}
        service: {{ service }}
        running: {{ running }}
{% endif %}

This works as expected but I wanted to know if anything sticks out like "that's not how any of this works" kind of a thing.

Specifically, for the if statement:

{% if data[data['service_name']]['running'] == False %}

Is there a way to reuse "running" to say something like:

{% if {{ running }} == False %}

Or something like that? I probably shouldn't use data[data['service_name']]['running'] twice in the same sls file right?

Thanks again

evenyougreg avatar Jun 04 '19 21:06 evenyougreg

I figured out that this works:

{% if not running %}

I think my last question/concern is this:

Since Salt recommends that all services be dumped into the same single beacon configuration, that means (if I push the beacons using pillar data) the servers that need to leverage the service beacon mechanism will all receive the same list of services to monitor by virtue of that beacon configuration,... correct? If so, then in my case there will be a lot of servers that are being asked to monitor services that are not present on that OS, for which there will be an error or warning every second for every service that does not exist on any given server. Is the solution to not use pillar data and use a catered beacons.conf file?

Thanks

evenyougreg avatar Jun 04 '19 22:06 evenyougreg