<jupyter_start><jupyter_text>StableNet® WeatherMap Handling IntroductionThis script gives a simple example on how to    Load a WeatherMap from StableNet® using the REST API    Enhance the WeatherMap with advanced REST API calls to obtain alarms filtered for the selected element and add them as statistics    "Re-Add" the WeatherMap to the server as enhanced version using the REST API once more.Import necessary python modules<jupyter_code>import warnings
import requests
from requests.auth import HTTPBasicAuth
import getpass
from xml.etree import ElementTree<jupyter_output><empty_output><jupyter_text>Enter Server Credentials & WeatherMapID to be used as base<jupyter_code>weathermapid = "1058"

#Credentials
server_ip = '10.20.20.113'
server_port = '5443'
username = 'infosim'<jupyter_output><empty_output><jupyter_text>Get Weather Map from Server and save XML to variable<jupyter_code>warnings.filterwarnings("ignore")
resp = requests.get("https://"+server_ip+":"+server_port+"/rest/weathermaps/get/" + weathermapid, 
                    verify=False, auth=HTTPBasicAuth(username, getpass.getpass('Enter password:')))
tree = ElementTree.fromstring(resp.content)<jupyter_output>Enter password: ··········<jupyter_text>Adding Alarms as Node Statistics to all Weather Map nodes<jupyter_code>pw=getpass.getpass('Enter password:');
for child in tree.findall('weathermapnodes/weathermapnode'):
    
	erefID=child.find('elementreference').get('obid')
	erefDOMAIN=child.find('elementreference').get('domain')
	stats=child.find('statistics')
	
	filter = ''
	if erefDOMAIN=="device": 
		filter='<valuetagfilter filtervalue="'+erefID+'"><tagcategory key="Device ID"/></valuetagfilter>'
	if erefDOMAIN=="measurement": 
		filter='<valuetagfilter filtervalue="'+erefID+'"><tagcategory key="Measurement ID"/></valuetagfilter>'
	if erefDOMAIN=="link": 
		filter='<valuetagfilter filtervalue="'+erefID+'"><tagcategory key="Link ID"/></valuetagfilter>'
	filter='<openalarmfilter>'+filter+'</openalarmfilter>'
	print('[Adding alarms for '+erefDOMAIN+' element with ID ' + erefID+']', end='')
	resp=requests.post("https://"+server_ip+":"+server_port+"/rest/events/liveopenalarms", 
                     verify=False, auth=HTTPBasicAuth(username, pw), 
                     data=filter, headers={'Content-Type': 'application/xml'})
	alarms = ElementTree.fromstring(resp.content)
    
	for openalarm in alarms:
		alarminfo = openalarm.find('rootcause').get('info')
		monitorid = openalarm.find('rootcause').get('monitorid')            
		print('R', end='')
		# Create statistic entry
		newentry=ElementTree.SubElement(stats,'statistic',{'showaslabel': 'false', 'type': 'monitorvalue', 'title': '[ROOT CAUSE] '+alarminfo})
		ElementTree.SubElement(newentry,'reference', {'obid': monitorid, 'domain': 'monitor'})
		ElementTree.SubElement(newentry,'time', {'multiplier': '1440', 'type': 'lastminutes', 'timezone': 'Europe/Berlin', 'average': '60000'})
		for symptom in openalarm.findall('symptoms/symptom'):
			alarminfo = symptom.get('info')
			monitorid = symptom.get('monitorid')            
			print('S', end='')
			# Create statistic entry
			newentry=ElementTree.SubElement(stats,'statistic',{'showaslabel': 'false', 'type': 'monitorvalue', 'title': '[SYMPTOM] '+alarminfo})
			ElementTree.SubElement(newentry,'reference', {'obid': monitorid, 'domain': 'monitor'})
			ElementTree.SubElement(newentry,'time', {'multiplier': '1440', 'type': 'lastminutes', 'timezone': 'Europe/Berlin', 'average': '60000'})
	print('')

tree.set('name',tree.get('name')+' (Alarm Statistics)')
        
finalMap = ElementTree.tostring(tree)<jupyter_output>Enter password: ··········<jupyter_text>Adding Extended Weather Map to server as new Weather Map<jupyter_code>warnings.filterwarnings("ignore")
resp=requests.post("https://"+server_ip+":"+server_port+"/rest/weathermaps/add/", 
                     verify=False, auth=HTTPBasicAuth(username, getpass.getpass('Enter password:')), 
                     data=finalMap, headers={'Content-Type': 'application/xml'})<jupyter_output>Enter password: ··········
StarcoderdataJupyterStructuredCleanDedup