Reload Telegraf configs on Windows

I've upgraded a couple of Windows PCs to the latest GA of Telegraf (v1.14), and noticed that the Telegraf service is sometimes not starting. Without investigating the log entries, my hypothesis is that it's starting before networking has completed successfully (enabling the adaptor, starting the TCP/IP stack, getting an IP address from DHCP, etc), so my "Continuous Deployment of Telegraf Config" blog post is failing for these PCs. Additionally, Telegraf will sometimes not restart if a Windows PC restores from hibernate mode.

Windows has a task scheduler. If I can create a scheduled Windows task to replicate what I'm doing in Linux, Telegraf will be able to periodically reload configs and I'll miss much less data.

Windows Task Scheduler can run tasks on startup, on login, or on a schedule. For now, I'll run on a schedule, starting at 3am and run it every hour. It will also try to run immediately if it hasn't triggered since boot, and it will completely reload the task at 3am.

I don't see a way in Windows Task Scheduler to send a signal to reload Telegraf configurations (SIGHUP), so this utility uses the built in Windows "net" command to stop and then start Telegraf.

Save this XML as "telegraf reload.xml" on the Windows PC.
Run "Task Scheduler".
In the right pane, select "import task", and open the file you just created.
You can run the task now if you want to test it.
It runs as the SYSTEM user, so it won't open a visible cmd window, and you'll need to either check your running services or check your NodeRED server to see that it worked.

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2020-05-23T14:56:44.8071946</Date>
    <Author>INFLUXDATA\thisispoki</Author>
    <Description>This scheduled task will restart Telegraf on Windows 10 every hour, with a random delay of less than 1 minute. This task will reload every day at 3am</Description>
    <URI>\Telegraf reload</URI>
  </RegistrationInfo>
  <Triggers>
    <CalendarTrigger>
      <Repetition>
        <Interval>PT1H</Interval>
        <Duration>P1D</Duration>
        <StopAtDurationEnd>false</StopAtDurationEnd>
      </Repetition>
      <StartBoundary>2020-05-23T03:00:00</StartBoundary>
      <Enabled>true</Enabled>
      <RandomDelay>PT1M</RandomDelay>
      <ScheduleByDay>
        <DaysInterval>1</DaysInterval>
      </ScheduleByDay>
    </CalendarTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <UserId>S-1-5-18</UserId>
      <RunLevel>LeastPrivilege</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
    <AllowHardTerminate>false</AllowHardTerminate>
    <StartWhenAvailable>true</StartWhenAvailable>
    <RunOnlyIfNetworkAvailable>true</RunOnlyIfNetworkAvailable>
    <IdleSettings>
      <StopOnIdleEnd>true</StopOnIdleEnd>
      <RestartOnIdle>false</RestartOnIdle>
    </IdleSettings>
    <AllowStartOnDemand>true</AllowStartOnDemand>
    <Enabled>true</Enabled>
    <Hidden>false</Hidden>
    <RunOnlyIfIdle>false</RunOnlyIfIdle>
    <DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
    <UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>
    <WakeToRun>false</WakeToRun>
    <ExecutionTimeLimit>PT0S</ExecutionTimeLimit>
    <Priority>7</Priority>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>net</Command>
      <Arguments>stop telegraf</Arguments>
    </Exec>
    <Exec>
      <Command>net</Command>
      <Arguments>start telegraf</Arguments>
    </Exec>
  </Actions>
</Task>

Comments

Popular Posts