It’s quite typical to have several managed Windows Servers joined to a Windows Active Directory Domain.
But how do you handle the automated installation and configuration of the monitoring agents? How do you keep them up to date?
DSC (Desired State Configuration) is a management platform in PowerShell that enables you to manage your IT and development infrastructure with configuration as code.
DSC is a declarative platform used for configuration, deployment, and management of systems. It consists of three primary components:
Desired State Configuration is included in Windows and updated through the Windows Management Framework. It’s already configured and available on Windows Server 2012 onward, and can be configured also on (the no longer supported) Windows Server 2008 R2 SP1.
Here is a sample DSC configuration taken from the Microsoft online documentation. It can be applied to the local node (as in this example “localhost”), or pushed to a remote one:
Configuration EnvironmentVariable_Path
{
param ()
Import-DscResource -ModuleName 'PSDscResources'
Node localhost
{
Environment CreatePathEnvironmentVariable
{
Name = 'TestPathEnvironmentVariable'
Value = 'TestValue'
Ensure = 'Present'
Path = $true
Target = @('Process', 'Machine')
}
}
}
EnvironmentVariable_Path -OutputPath:"C:\EnvironmentVariable_Path"
This configuration PowerShell script, when executed, will generate a .MOF file in the C:\EnvironmentVariable_Path. This MOF configuration can be then applied to the localhost through a Start-DscConfiguration command, and queried using the Get-DscConfiguration statement to verify whether the desired configuration matches.
A Practical Example of Using DSC: Deploying the telegraf Agent to Remote Windows Servers
In this first example, I will show you how to install the telegraf agent on remote Windows servers.
In the second part of this Blog, I will show you how to handle the Icinga2 agent remote installation.
Both agents can be used for remote monitoring by the NetEye server.
In this example I choose a Windows server as a configuration master and push all the configurations to the remote nodes. (DSC could also work in scheduled Pull mode, but it’s a little more difficult to set up).
On the Master server I will create a C:\DSC path and, under that, a couple of subdirectories: SW and MOF.
SW must be shared for reading by the remote computer accounts (use the built-in Domain Group) and will contain the telegraf binaries under SW\telegraf\ and a basic telegraf configuration file as SW\telegraf_cfg\telegraf-base.conf.
C:\DSC\MOF will receive the generated configurations. Here’s the PowerShell configuration script:
#Requires -module ComputerManagementDsc
<#
Test installation Telegraf
#>
Configuration Monitor
{
Import-DscResource –ModuleName 'PSDesiredStateConfiguration'
Node @("server1","server2","server3")
{
File DirectoryCopy
{
Ensure = "Present"
Type = "Directory"
Recurse = $true
SourcePath = "\\master\SW\telegraf"
DestinationPath = "c:\Program Files\telegraf"
MatchSource = $true
Checksum = "modifiedDate"
}
File TelCfgCopy
{
Ensure = "Present"
Type = "File"
SourcePath = "\\master\SW\telegraf_cfg\telegraf-base.conf"
DestinationPath = "c:\Program Files\telegraf\telegraf.conf"
MatchSource = $true
Checksum = "modifiedDate"
}
Service CreateService
{
Name = "telegraf"
Ensure = "Present"
Path = "c:\Program Files\telegraf\telegraf.exe"
Description = "Telegraf monitoring"
BuiltInAccount = "LocalSystem"
StartupType = "Automatic"
State = "Running"
}
}
}
Monitor -OutputPath:"C:\DSC\MOF"
In this script, “master” is the Master server name and server1, server2, server3 are the remote configured servers.
In the first step, it will copy the telegraf binaries, then the telegraf configuration file and finally create and start the telegraf service.
At every execution, the script will check the timestamp of all the files and copy them again if changed.
When I execute this script on the master node, it will generate 3 MOF files in the path C:\DSC\MOF.
These configurations can then be applied (pushed) with a simple PowerShell command:
Start-DscConfiguration -Path 'C:\DSC\MOF' -Wait
DSC is a very powerful framework, allowing you to control every aspect of a Windows machine. DSC is designed to maintain the desired configurations! It’s not just an installation language.
If used in the right way, DSC can handle any configuration changes, reducing the configuration time to a few seconds. More information can be found on the Microsoft online documentation: