Last active
August 29, 2015 14:10
-
-
Save remh/d27cea667614c19afa41 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from checks.network_checks import NetworkCheck, Status | |
import subprocess | |
class PowerShell(NetworkCheck): | |
SOURCE_TYPE_NAME = "powershell" | |
SERVICE_CHECK_NAME = "powershell.ran_sucessfully" | |
def _load_conf(self, instance): | |
interpreter = instance.get('interpreter') | |
if interpreter is None: | |
raise Exception("You must specify the path to the Powershell interpreter") | |
script = instance.get("script") | |
if script is None: | |
raise Exception("You must specify a script to run") | |
params = instance.get("params") | |
if params is None: | |
params = [] | |
if type(params) == str: | |
params = params.split() | |
return interpreter, script, params, instance.get("name") | |
def _check(self, instance): | |
try: | |
interpreter, script, params, name = self._load_conf(instance) | |
command = [interpreter, script] + params | |
self.log.debug("Running {0}".format(" ".join(command))) | |
proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
err = proc.stderr.read() | |
if err: | |
self.log.error("Error while running command: {0}. {1}".format(" ".join(command), err)) | |
return Status.DOWN, err | |
else: | |
output = proc.stdout.read() | |
self.log.debug("Script returned: {0}".format(output)) | |
return Status.UP, output | |
except Exception, e: | |
self.log.exception(e) | |
return Status.DOWN, str(e) | |
def report_as_service_check(self, name, status, instance, msg=None): | |
interpreter, script, params, name = self._load_conf(instance) | |
command = " ".join([interpreter, script] + params) | |
status = NetworkCheck.STATUS_TO_SERVICE_CHECK[status] | |
self.log.debug("Sending service check {0} with status {1}".format( | |
self.SERVICE_CHECK_NAME, status)) | |
self.service_check(self.SERVICE_CHECK_NAME, | |
status, | |
tags=["command:{0}".format(command), "script_name:{0}".format(name)], | |
message = msg | |
) | |
def _create_status_event(self, status, msg, instance): | |
pass |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
init_config: | |
instances: | |
- name: myscript | |
interpreter: C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe | |
script: C:\Users\vagrant\Desktop\test.ps1 | |
params: | |
- param1 | |
- param2 | |
- ... | |
skip_event: True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add powershell.py in C:\ProgramData\Datadog\checks.d
Add powershell.yaml in C:\ProgramData\Datadog\conf.d and customize
Restart the agent