-
-
Notifications
You must be signed in to change notification settings - Fork 43
Infrastructure As Code
- Object Model
- Tree Creation
- Infrastructure As Code
- Tree Manipulation
Programming libraries provide users with the tools they need in order to accomplish a given task. If you want to create a new device in PRTG - but only if it doesn't already exist - you can easily do so: simply combine an if statement, the Get-Device command and the Add-Device command and you've got yourself some control flow!
As you start to add more requirements however ("and my device needs to have these tags, with this notification trigger underneath it, and these sensors") such imperative programming starts to get a bit more complicated, not to mention tedious. All of the tools you need to achieve your task are right in front of you, but adding all the required logic and error handling as your program grows in scope can add up to a lot of work.
To combat this complexity, infrastructure as code tools such as Terraform and Desired State Configuration have evolved, allowing you to describe the desired state you would like to achieve, without having to worry about how to accomplish that task, or even whether it has already been done.
In this article, we will see how PrtgAPI's Tree API can be leveraged for achieving true PowerShell based infrastructure as code, enabling you to
- Generate PrtgAPI infrastructure as code configuration files from existing PRTG objects
- Manage your entire PRTG configuration from a single configuration file
- Migrate objects between PRTG servers
- and more
In PowerShell, a pair of curly brackets is used to denote a block of code
function foo
{
if ($true)
{
# do something
}
}In certain contexts however, this block of code can be interpreted as being a ScriptBlock - a definition of some code that you would lazily like to execute on demand at some later point in time
Get-Sensor | where { $_.Name -eq "Ping" }Typically these script blocks are used for describing relatively short snippets of code that should be executed by cmdlets such as Where-Object or Invoke-Command. By designing your code around script blocks however, it is possible to blur the lines between what is a ScriptBlock and what isn't, enabling a style of declarative programming, such as is seen in PowerShell libraries such as Pester and PrtgXml
# Create a tree consisting of a the "Local Probe" probe, the "dc-1" device
# beneath it, and all of dc-1's child sensors
ProbeNode "Local Probe" {
DeviceNode "dc-1" {
SensorNode *
}
}Utilizing this technique, it is possible for us to describe and apply our desired PRTG state all from a single configuration file, all within PowerShell.