Project: Chef InSpec (Provisioning / Security & Compliance)
File: service.md

+++ title = "service resource" draft = false gh_repo = "inspec" platform = "os" [menu] [menu.inspec] title = "service" identifier = "inspec/resources/os/service.md service resource" parent = "inspec/resources/os" +++ Use the `service` Chef InSpec audit resource to test whether the installation of the named service is successful and enabled. It may be necessary to specify the service manager by using one of the following service manager-specific resources: `bsdservice`, `launchdservice`, `runitservice`, `systemdservice`, `sysvservice`, and `upstartservice`. These resources are based on the `service` resource. The Chef InSpec distributes this resource. This resource is available from Chef Inspec version 1.0.0. A `service` resource block declares the name of a service and one or more matchers to test the service state. ```ruby describe service('NAME') do it { should be_installed } it { should be_enabled } it { should be_running } end ``` where `('service_name')` must specify a service name `beinstalled`, `beenabled`, and `be_running` are valid matchers for this resource The following examples show how to use this Chef InSpec audit resource. ```ruby describe service('PostgreSQL') do it { should be_enabled } it { should be_running } end ``` ```ruby describe service('MYSQL') do it { should be_enabled } it { should be_running } end ``` ```ruby describe package('clamav') do it { should be_installed } its('version') { should eq '0.98.7' } end describe service('clamd') do it { shouldnot beenabled } it { shouldnot beinstalled } it { shouldnot berunning } end ``` On targets that are using SystemV services, the existing run levels can also be checked: ```ruby describe service('SSH').runlevels do its('keys') { should include(2) } end describe service('SSH').runlevels(2,4) do it { should be_enabled } end ``` It may be required to override the logic to select the right service manager. For example, to check a service managed by Upstart. ```ruby describe upstart_service('SERVICE') do it { shouldnot beenabled } it { should be_installed } it { should be_running } end ``` This is also possible with `systemdservice`, `runitservice`, `sysvservice`, `bsdservice`, and `launchd_service`. If not found at the default location, provide the control command. For example, if the `sv` command for services managed by `runit` is not in the `PATH`. ```ruby describe runit_service('SERVICE', '/opt/chef/embedded/sbin/sv') do it { should be_enabled } it { should be_installed } it { should be_running } end ``` ```ruby describe service('W3SVC') do it { should be_installed } it { should be_running } end ``` For a full list of available matchers, please visit the . The `be_enabled` matcher tests if the named service is enabled: ```ruby it { should be_enabled } ``` The `be_installed` matcher tests if the named service is installed. ```ruby it { should be_installed } ``` The `be_running` matcher tests if the named service is running. ```ruby it { should be_running } ``` The `bemonitoredby` matcher accepts the name of a monitoring tool as input and test if the named service is monitored respectively. The monitoring tool supports `monit` and `god` resources. ```ruby it { should bemonitoredby("god") } ``` The `havestartmode` matcher tests accept a mode as input and test if the named service's start mode is the same as specified in the input. This matcher is supported on the Windows systems only. ```ruby it { should havestartmode('Manual') } ```