def hello(event, context): s3 = boto3.resource('s3') s3Obj = s3.Object('apartment-search', 'apartmentList.json') apartmentUrl = [ 'https://www.on-site.com/web/online_app/choose_unit?goal=6&attr=x20&property_id=286650&lease_id=0&unit_id=0&required=', ] page = requests.get(apartmentUrl[0]) soup = BeautifulSoup(page.content, 'html.parser') apartmentList = json.loads(s3Obj.get()['Body'].read().decode('utf-8')) apartments = soup.findAll("tr", {"class": "unit_display"}) resp = [] for apt in apartments: if (apt['data-apartment-number'] not in apartmentList.keys()): resp.append(sendEmail(apt)) apartmentList[apt['data-apartment-number']] = None s3Obj.put(Body=(bytes(json.dumps(apartmentList).encode('UTF-8')))) return '/n'.join(resp)
applianceMetrics = json.loads(the_page) return applianceMetrics if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("-d", "--disconnect_percentage", help="The amount of disconnected PVs we are willing to tolerate (in percentage)", default=MAX_DISCONN_PERCENTAGE, type=float) parser.add_argument("url", help="This is the URL to the mgmt bpl interface of the appliance cluster. For example, http://arch.slac.stanford.edu/mgmt/bpl") args = parser.parse_args() if not args.url.endswith('bpl'): print "The URL needs to point to the mgmt bpl; for example, http://arch.slac.stanford.edu/mgmt/bpl. ", args.url sys.exit(1) disconnected_precentage = args.disconnect_percentage applianceMetrics = getApplianceMetrics(args.url) if applianceMetrics: emailMsg = "Disconnected PVs in {0}\n".format(args.url) sendEmail= False for applianceMetric in applianceMetrics: connectedPVs = int(applianceMetric['connectedPVCount']) disconnectedPVs = int(applianceMetric['disconnectedPVCount']) totalPVs = connectedPVs + disconnectedPVs if((disconnectedPVs*100.0)/totalPVs) > disconnected_precentage: sendEmail = True emailMsg = emailMsg + "{0} of {1} PVs in appliance {2} are in a disconnected state\n".format(disconnectedPVs, totalPVs, applianceMetric['instance']) if sendEmail: emailHandler.sendEmail("Disconnected PVs in " + args.url, emailMsg, []) else: print "Cannot obtain appliance metrics" emailHandler.sendEmail("checkConnectedPVs", "Cannot obtain appliance metrics from " + args.url, [])
def getTypeChangedPVs(bplURL): '''Get a list of PVs that changed type''' url = bplURL + '/getPVsByDroppedEventsTypeChange' req = urllib2.Request(url) response = urllib2.urlopen(req) the_page = response.read() typeChangedPVs = json.loads(the_page) return typeChangedPVs if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument( "url", help= "This is the URL to the mgmt bpl interface of the appliance cluster. For example, http://arch.slac.stanford.edu/mgmt/bpl" ) args = parser.parse_args() if not args.url.endswith('bpl'): print "The URL needs to point to the mgmt bpl; for example, http://arch.slac.stanford.edu/mgmt/bpl. ", args.url sys.exit(1) typeChangedPVs = getTypeChangedPVs(args.url) if typeChangedPVs: print len(typeChangedPVs), "PVs have changed type" emailHandler.sendEmail("PV Type changes", "Some PVs have changed type in " + args.url, [x['pvName'] for x in typeChangedPVs]) else: print "No PVs have changed type"
import datetime import time import operator import emailHandler def getPVsWithEstimatedStorageGreaterThan(bplURL, limitInGbPerYear, limit): """ Get a dict of PV to """ resp = requests.get(bplURL + '/getStorageRateReport', params={"limit": limit}) resp.raise_for_status() pv2s = { x["pvName"] : float(x["storageRate_GBperYear"]) for x in resp.json()} gpv2s = { k : v for k,v in pv2s.items() if v > limitInGbPerYear } return sorted(gpv2s.items(), key=operator.itemgetter(1), reverse=True) if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("url", help="This is the URL to the mgmt bpl interface of the appliance cluster. For example, http://arch.slac.stanford.edu/mgmt/bpl") parser.add_argument("maxsize", help="Report any PV's archiving at a rate greater than this rate. This is specified in GB/year.", type=float) parser.add_argument("--limit", help="Limit the number of entries in the report.", type=int, default=100) args = parser.parse_args() bplURL = args.url + "/" if not args.url.endswith("/") else args.url emailMsg = "PVs with estimated storage greater than {1}GB/year in {0}\n".format(bplURL, args.maxsize) sendEmail = False for (pv, gbperyear) in getPVsWithEstimatedStorageGreaterThan(bplURL, args.maxsize, args.limit): emailMsg = emailMsg + "PV: {} Size(GB/year): {}\n".format(pv, gbperyear) sendEmail = True if sendEmail: print("Sending email...") emailHandler.sendEmail("PVs with excessive storage rate in " + args.url, emailMsg, [])
import urllib import urllib2 import json import datetime import time import emailHandler def getTypeChangedPVs(bplURL): '''Get a list of PVs that changed type''' url = bplURL + '/getPVsByDroppedEventsTypeChange' req = urllib2.Request(url) response = urllib2.urlopen(req) the_page = response.read() typeChangedPVs = json.loads(the_page) return typeChangedPVs if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("url", help="This is the URL to the mgmt bpl interface of the appliance cluster. For example, http://arch.slac.stanford.edu/mgmt/bpl") args = parser.parse_args() if not args.url.endswith('bpl'): print "The URL needs to point to the mgmt bpl; for example, http://arch.slac.stanford.edu/mgmt/bpl. ", args.url sys.exit(1) typeChangedPVs = getTypeChangedPVs(args.url) if typeChangedPVs: print len(typeChangedPVs), "PVs have changed type" emailHandler.sendEmail("PV Type changes", "Some PVs have changed type in " + args.url, [x['pvName'] for x in typeChangedPVs]) else: print "No PVs have changed type"