Last active
July 14, 2021 19:08
-
-
Save trir262/cf740eda6c4ab4cccdeefba87f5d7d3d to your computer and use it in GitHub Desktop.
Share results between snippets in a runbook
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 silo_common.database import local_db | |
from collections import namedtuple | |
#Anthing passed via the EM7_RESULT is in clear text visible to anyone. Better not send any confidential data through that channel! | |
app_id = 0 | |
try: | |
dbc = local_db() | |
query = ("select app_id from master.dynamic_app_alerts where alert_id = {}".format(EM7_VALUES['%F'])) | |
app_id = dbc.autofetch_value(query) | |
except: | |
pass | |
EM7_RESULT = {'aiddiscovery' : {'application_id':app_id}} |
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 silo_common.snippets.powershell import powershell_winrm | |
from collections import namedtuple | |
EM7_RESULT={} | |
for last_result in EM7_LAST_RESULT_LIST: | |
if 'aiddiscovery' in last_result.result.keys(): | |
app_id = last_result.result['aiddiscovery']['application_id'] | |
if app_id is not None and app_id != 0 and app_id in EM7_DYNAMIC_APP_CREDS.keys(): | |
try: | |
logger=em7_snippets.logger(filename='/data/logs/restart_windows_service.log') | |
except: | |
pass | |
cred_details = EM7_DYNAMIC_APP_CREDS[app_id] | |
ps_request = "Get-Service -Name '{servicename}' | ForEach-Object {{ Start-Service $_.Name -PassThru; $_.DependentServices | Start-Service -PassThru}} | Select Name, DisplayName, Status".format(servicename = EM7_VALUES['%V']) | |
Request = namedtuple('Request', 'req_id key_column request app_id') | |
request = Request(-1, 'RestartService', ps_request, app_id) | |
data, error = powershell_winrm(EM7_VALUES['%x'], cred_details['cred_host'], request, cred_details, True, None, None, logger) | |
EM7_RESULT['windowsservice']={'name':EM7_VALUES['%V'], 'status':'SUCCESS'} | |
if error is not None and len(error) > 0: | |
EM7_RESULT['outmail']={'mailsubject': 'Automated Service Restart event', 'mailtemplate': '30', 'mailresult':'FAIL', 'mailargs' : {'ServiceName' : EM7_VALUES['%V'], 'Result' : 'Stopped' } } | |
else: | |
EM7_RESULT['outmail']={'mailsubject': 'Automated Service Restart event', 'mailtemplate': '30', 'mailresult':'SUCCESS', 'mailargs' : {'ServiceName' : EM7_VALUES['%V'], 'Result' : 'Started' }} | |
else: | |
EM7_RESULT['outmail']={'mailsubject': 'Automated Service Restart event', 'mailtemplate': '30', 'mailresult':'FAIL', 'mailargs' : {'ServiceName' : EM7_VALUES['%V'], 'Result' : 'Stopped' }} |
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 silo_common.database import local_db | |
from datetime import datetime | |
from collections import namedtuple | |
from silo_utils import silo_api, APIError | |
import requests, json | |
EM7_RESULT={} | |
EM7_RESULT['log']=[] | |
EM7_RESULT['outmail'] = [] | |
outmail=False | |
try: | |
for last_result in EM7_LAST_RESULT_LIST: | |
if 'outmail' in last_result.result.keys(): | |
outmail = True | |
try: | |
api_instance = silo_api(EM7_ACTION_CRED) | |
if not api_instance: | |
raise APIError("Could not open API") | |
try: | |
api_res = api_instance.get('api/device/{}'.format(EM7_VALUES['%x'])) | |
api_res = api_instance.get( (api_res.json())['organization'] ) | |
mailbcc= (api_res.json())['email'] | |
except: | |
mailbcc='' | |
pass | |
mailtemplateid=last_result.result['outmail']['mailtemplate'] | |
mailsubject=last_result.result['outmail']['mailsubject'] | |
mailargs = last_result.result['outmail']['mailargs'] | |
if 'windowsservice'in last_result.result.keys(): | |
servicename=last_result.result['windowsservice']['name'] | |
servicestatus=last_result.result['windowsservice']['status'] | |
except Exception, exe: | |
EM7_RESULT['log'].append((5,'Error "{}" while processing last_result_list'.format(exe))) | |
pass | |
if outmail: | |
try: # get mail template | |
dbc = local_db() | |
query = ("select template_body from master_biz.document_templates where doc_id = {}".format(mailtemplateid)) | |
mailbody = dbc.autofetch_value(query) | |
try: # insert into out_messages.spool_mail | |
vals={} | |
vals['message']='<html><body>' + (mailbody.format(**mailargs))[:65500] + '</body></html>' | |
vals['subject']=mailsubject | |
# vals['recip']=mailto | |
# vals['cc']=mailcc | |
vals['originator']='"No Reply" <[email protected]>' | |
vals['bcc']=mailbcc | |
vals['roa_id']=1 | |
vals['priority']=3 | |
vals['state']=0 | |
vals['eid']=EM7_VALUES['%e'] | |
vals['submit_stamp']=datetime.now().strftime("%Y-%m-%d %H:%M:%S") | |
vals['send_stamp']='0000-00-00 00:00:00' | |
vals['rt_processed']=0 | |
dbc.autoexec_insert(table='out_messages.spool_mail',keyvals=vals) | |
EM7_RESULT['outmail'].append(('Message for {} success queued for delivery'.format(mailto))) | |
EM7_RESULT['log'].append((20,'Successfully Queued message')) | |
except Exception, exe: | |
EM7_RESULT['outmail'].append(('Error "{}" occurred inserting into mail table'.format(exe))) | |
pass | |
except Exception, exe: | |
EM7_RESULT['log'].append((10,'Error "{}" occurred getting mail template "{}"'.format(exe, mailtemplateid))) | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment