def set_react_role_message(self, message_id): ''' Adds/Updates the current ract_role_message_id. Args: - message_id (int): ID of message from react_role. Returns: None. ''' with self.session() as session: guild_query = session.query(AdminOptions).filter( AdminOptions.guild_id == self.guild_id).one_or_none() # if there are already entries for this guild, updates them if guild_query is not None: guild_query.ract_role_message_id = str(message_id) session.commit() # if there are no entries for this guild, creates entry else: admin_options = AdminOptions() admin_options.guild_id = self.guild_id admin_options.ract_role_message_id = str(message_id) session.add(admin_options) session.commit()
def set_welcome_channel(self, home_msg_id): ''' Adds/Updates the current home channel msg in the database. Args: - home_msg_id (int): ID of home channel message. Returns: None. ''' home_msg_id = str(home_msg_id) with self.session() as session: guild_query = session.query(AdminOptions).filter( AdminOptions.guild_id == self.guild_id).one_or_none() # if there are already entries for this guild, updates them if guild_query is not None: guild_query.home_msg_id = home_msg_id session.commit() # if there are no entries for this guild, creates entry else: admin_options = AdminOptions() admin_options.guild_id = self.guild_id admin_options.home_msg_id = home_msg_id session.add(admin_options) session.commit()
def remove_welcome_channel(self): ''' Removes the current home channel msg in the database. Args: None. Returns: None. ''' with self.session() as session: guild_query = session.query(AdminOptions).filter( AdminOptions.guild_id == self.guild_id).one_or_none() # if there are already entries for this guild, updates them if guild_query is not None: if guild_query.home_msg_id: guild_query.home_msg_id = "0" session.commit() # if there are no entries for this guild, creates entry else: admin_options = AdminOptions() admin_options.guild_id = self.guild_id admin_options.home_msg_id = "0" session.add(admin_options) session.commit()
def update_default_role(self, default_role_id): ''' Adds/Updates the current default role in the database. Args: - default_role_id (int): ID of default role. Returns: None. ''' str_default_role_id = str(default_role_id) with self.session() as session: guild_query = session.query(AdminOptions).filter( AdminOptions.guild_id == self.guild_id).one_or_none() # if there are already entries for this guild, updates them if guild_query is not None: if guild_query.default_role_id: guild_query.default_role_id = str_default_role_id session.commit() # if there are no entries for this guild, creates entry else: admin_options = AdminOptions() admin_options.guild_id = self.guild_id admin_options.default_role_id = str_default_role_id session.add(admin_options) session.commit()
def post(self): adminoptions = AdminOptions(key_name="credentials") adminoptions.twitteruser = self.request.get('twitteruser') adminoptions.twitterpass = self.request.get('twitterpass') adminoptions.prowlkey = self.request.get('prowlkey') prowlnotifier = prowlpy.Prowl(self.request.get('prowlkey')) try: adminoptions.prowlkeyisvalid = prowlnotifier.verify_key() except: adminoptions.prowlkeyisvalid = False adminoptions.put() self.redirect('/admin')
def get(self): adminoptions = AdminOptions.get_by_key_name('credentials') if adminoptions: monitoroptions = EC2PricingMonitor.get_by_key_name(adminoptions.accountname) if monitoroptions: user = users.get_current_user() input_data_settings = {} for instance in self.instances2var.keys(): url = "http://www.cloudexchange.org/data/"+instance+".csv" logging.info("fetching from URL: %s" % url) try: result = urlfetch.fetch(url) logging.info("return code : %s" % result.status_code) except: logging.info("got exception from urlfetch") if result.status_code == 200: content = result.content logging.info("content : %s" % content) input_data_settings[instance] = r"<settings><data_sets><data_set did='0'><csv><data>%s</data></csv></data_set></data_sets></settings>" % content.replace('\n',','+self.instances_privcost[instance]+','+self.instances_basiccost[instance]+'\\n') logging.info("input_data_settings: %s" % input_data_settings[instance]) else: input_data_settings[instance] = r"<settings><data_sets><data_set did='0'><csv><data>2009-11-30 21:21:21,0.029\n2009-12-01 05:12:36,0.029\n2009-12-01 05:12:37,0.03\n2009-12-01 09:59:04,0.03</data></csv></data_set></data_sets></settings>" logging.info("input_data_settings: %s" % input_data_settings[instance]) template_values = {} for instance,variable in self.instances2var.iteritems(): template_values[variable] = input_data_settings[instance] template_values['monitoroptions'] = monitoroptions # template_values = {'eu_west_small_linux_data': input_data_settings['eu-west-1.linux.m1.small'],'us_west_small_linux_data': input_data_settings['us-west-1.linux.m1.small'],'us_east_small_linux_data': input_data_settings['us-east-1.linux.m1.small'],} path = os.path.join(os.path.dirname(__file__), 'dashboard.html') self.response.out.write(template.render(path, template_values)) else : self.redirect('/admin') else : self.redirect('/admin')
def post(self): adminoptions = AdminOptions(key_name="credentials") logging.info('Post for Account %s' % self.request.get('accountname')) if self.request.get('accountname'): account = EC2Account(key_name=self.request.get('accountname')) adminoptions.accountname = self.request.get('accountname') account.ec2apikey = self.request.get('ec2apikey') account.ec2secretkey = self.request.get('ec2secretkey') # EC2 = get_driver(Provider.EC2) # driver = [ EC2(account.ec2apikey,account.ec2secretkey) ] # try: # nodes = driver.list_nodes() # except: # logging.info('Invalid EC2 credentials for Account : %s with exception: %s' % (self.request.get('accountname'),sys.exc_type)) # self.redirect('/admin') # return account.put() adminoptions.put() self.redirect('/admin')
def add_react_role(self, react_role_dict): ''' Adds/Updates the current ract_role_dictionary. Args: - react_role_dict (dict): dictionary in the format: { "react_emote": role_id, } Returns: None. ''' with self.session() as session: guild_query = session.query(AdminOptions).filter( AdminOptions.guild_id == self.guild_id).one_or_none() # if there are already entries for this guild, updates them if guild_query is not None: if guild_query.react_role_dict: curr_dict = json.loads(guild_query.react_role_dict) curr_dict.update(react_role_dict) guild_query.react_role_dict = json.dumps(curr_dict) session.commit() else: guild_query.react_role_dict = json.dumps(react_role_dict) session.commit() # if there are no entries for this guild, creates entry else: admin_options = AdminOptions() admin_options.guild_id = self.guild_id admin_options.react_role_dict = json.dumps(react_role_dict) session.add(admin_options) session.commit()
def add_cursed_words(self, words): ''' Adds/Updates the current list of cursed words. Args: - words (lst): List of words to add to cursed_words. Returns: None. ''' with self.session() as session: guild_query = session.query(AdminOptions).filter( AdminOptions.guild_id == self.guild_id).one_or_none() # if there are already entries for this guild, updates them if guild_query is not None: if guild_query.cursed_words: new_words = set(words + guild_query.cursed_words.split(',')) guild_query.\ cursed_words = ','.join(new_words) session.commit() else: guild_query.cursed_words = ','.join(set(words)) session.commit() # if there are no entries for this guild, creates entry else: admin_options = AdminOptions() admin_options.guild_id = self.guild_id admin_options.cursed_words = ','.join(set(words)) session.add(admin_options) session.commit()
def get(self): adminoptions = AdminOptions.get_by_key_name('credentials') if adminoptions: twitterpass = adminoptions.twitterpass twitteruser = adminoptions.twitteruser prowlkey = adminoptions.prowlkey else: twitterpass = "******" twitteruser = "******" prowlkey = "Change Me" serverlist = db.GqlQuery("SELECT * FROM Server") user = users.get_current_user() template_values = {'user': user, 'twitteruser': twitteruser, 'twitterpass': twitterpass, 'serverlist': serverlist, 'prowlkey': prowlkey, 'adminoptions': adminoptions,} path = os.path.join(os.path.dirname(__file__), 'admin.html') self.response.out.write(template.render(path, template_values))
def get(self): adminoptions = AdminOptions.get_by_key_name('credentials') if adminoptions: twitterpass = adminoptions.twitterpass twitteruser = adminoptions.twitteruser prowlkey = adminoptions.prowlkey monitoroptions = EC2PricingMonitor.get_by_key_name(adminoptions.accountname) logging.info("accountname = %s" % adminoptions.accountname) else: twitterpass = "******" twitteruser = "******" prowlkey = "Change Me" monitoroptions = EC2PricingMonitor(); serverlist = db.GqlQuery("SELECT * FROM Server") user = users.get_current_user() template_values = {'user': user, 'twitteruser': twitteruser, 'twitterpass': twitterpass, 'serverlist': serverlist, 'prowlkey': prowlkey, 'adminoptions': adminoptions,'monitoroptions': monitoroptions} path = os.path.join(os.path.dirname(__file__), 'admin.html') self.response.out.write(template.render(path, template_values))
def get(self): adminoptions = AdminOptions.get_by_key_name("credentials") if adminoptions: twitterpass = adminoptions.twitterpass twitteruser = adminoptions.twitteruser prowlkey = adminoptions.prowlkey else: twitterpass = "******" twitteruser = "******" prowlkey = "Change Me" serverlist = db.GqlQuery("SELECT * FROM Server") user = users.get_current_user() template_values = { "user": user, "twitteruser": twitteruser, "twitterpass": twitterpass, "serverlist": serverlist, "prowlkey": prowlkey, "adminoptions": adminoptions, } path = os.path.join(os.path.dirname(__file__), "admin.html") self.response.out.write(str(template.render(path, template_values)))
def post(self): adminoptions = AdminOptions.get_by_key_name("credentials") logging.info('Post for Account %s' % adminoptions.accountname) account = EC2PricingMonitor(key_name=adminoptions.accountname) account.privateinstanceutilization = int(self.request.get('utilization')) logging.info("checkbox output %s" % self.request.get('us_east_small_linux_valid')) if self.request.get('us_east_small_linux_valid') == "on": account.us_east_small_linux_valid = True else: account.us_east_small_linux_valid = False if self.request.get('us_west_small_linux_valid') == "on": account.us_west_small_linux_valid = True else: account.us_west_small_linux_valid = False if self.request.get('eu_west_small_linux_valid') == "on": account.eu_west_small_linux_valid = True else: account.eu_west_small_linux_valid = False if self.request.get('us_east_large_linux_valid') == "on": account.us_east_large_linux_valid = True else: account.us_east_large_linux_valid = False if self.request.get('us_west_large_linux_valid') == "on": account.us_west_large_linux_valid = True else: account.us_west_large_linux_valid = False if self.request.get('eu_west_large_linux_valid') == "on": account.eu_west_large_linux_valid = True else: account.eu_west_large_linux_valid = False account.put() self.redirect('/admin')
class CheckServers(webapp.RequestHandler): serverlist = db.GqlQuery("SELECT * FROM Server") adminoptions = AdminOptions.get_by_key_name('credentials') def updateuptime(self,server): now = time.mktime(datetime.datetime.now().timetuple()) servercameback = time.mktime(server.timeservercameback.timetuple()) difference = now - servercameback MINUTE = 60 HOUR = MINUTE * 60 DAY = HOUR * 24 days = int( difference / DAY ) hours = int( ( difference % DAY ) / HOUR ) minutes = int( ( difference % HOUR ) / MINUTE ) seconds = int( difference % MINUTE ) string = "" if days> 0: string += str(days) + " " + (days == 1 and "day" or "days" ) + ", " if len(string)> 0 or hours> 0: string += str(hours) + " " + (hours == 1 and "hour" or "hours" ) + ", " if len(string)> 0 or minutes> 0: string += str(minutes) + " " + (minutes == 1 and "minute" or "minutes" ) + ", " string += str(seconds) + " " + (seconds == 1 and "second" or "seconds" ) server.uptime = string server.put() def serverisup(self,server,responsecode): if server.status == False: self.servercameback(server) server.status = True server.falsepositivecheck = False server.responsecode = int(responsecode) server.uptimecounter = server.uptimecounter + 1 self.updateuptime(server) server.put() def serverisdown(self,server,responsecode): server.status = False server.uptimecounter = 0 server.uptime = "0" server.responsecode = int(responsecode) server.timeservercameback = 0 server.put() if server.notifylimiter == False: if server.notifywithprowl: self.notifyprowl(server) if server.notifywithemail: self.notifyemail(server) else: pass def servercameback(self,server): server.timeservercameback = datetime.datetime.now() def testserver(self,server): if server.ssl: prefix = "https://" else: prefix = "http://" try: url = prefix + "%s" % server.serverdomain result = urlfetch.fetch(url, headers = {'Cache-Control' : 'max-age=30'}, deadline=10 ) except DownloadError: if server.falsepositivecheck: self.serverisdown(server,000) else: server.falsepositivecheck = True else: if result.status_code == 500: self.serverisdown(server,result.status_code) else: self.serverisup(server,result.status_code) def notifyemail(self,server): message = mail.EmailMessage() message.sender = server.email message.subject = "%s is down" % server.serverdomain message.to = server.email message.body = "HTTP response code %s" % server.responsecode message.send() server.notifylimiter = True server.put() def notifytwitter(self,server): pass #api = twitter.Api(username="******" % self.adminoptions.twitteruser , password="******" % self.adminoptions.twitterpass) #api.PostDirectMessage(self.adminoptions.twitteruser, "%s is down" % server.serverdomain) #server.notifylimiter = True #server.put() def notifyprowl(self,server): prowlkey = self.adminoptions.prowlkey prowlnotifier = prowlpy.Prowl(prowlkey) try: prowlnotifier.add('Server Monitor','Server %s is Down' % server.serverdomain, 'error code %s' % server.responsecode) except: logging.error('prowl notify failed, you may need to check your API key') server.notifylimiter = True server.put() def get(self): for server in self.serverlist: self.testserver(server)
class CheckServers(webapp.RequestHandler): serverlist = db.GqlQuery("SELECT * FROM Server") adminoptions = AdminOptions.get_by_key_name('credentials') asyncfm = AsyncURLFetchManager() def updateuptime(self, server): now = time.mktime(datetime.datetime.now().timetuple()) servercameback = time.mktime(server.timeservercameback.timetuple()) difference = now - servercameback MINUTE = 60 HOUR = MINUTE * 60 DAY = HOUR * 24 days = int(difference / DAY) hours = int((difference % DAY) / HOUR) minutes = int((difference % HOUR) / MINUTE) seconds = int(difference % MINUTE) string = "" if days > 0: string += str(days) + "d " if len(string) > 0 or hours > 0: string += str(hours) + "h " if len(string) > 0 or minutes > 0: string += str(minutes) + "m " string += str(seconds) + "s" server.uptime = string def serverisup(self, server, responsecode): if server.status == False: self.servercameback(server) server.status = True server.falsepositivecheck = False server.parserstatus = True server.responsecode = int(responsecode) server.uptimecounter = server.uptimecounter + 1 self.updateuptime(server) server.put() def serverisdown(self, server, responsecode): server.status = False server.uptimecounter = 0 server.uptime = "0" server.responsecode = int(responsecode) server.timeservercameback = 0 server.put() if server.notifylimiter == False: if server.notifywithprowl: self.notifyprowl(server) if server.notifywithemail: self.notifyemail(server) else: pass def servercameback(self, server): server.timeservercameback = datetime.datetime.now() def testserver(self, server): if server.ssl: prefix = "https://" else: prefix = "http://" server.lastmonitor = datetime.datetime.now() url = prefix + "%s" % server.serverdomain logging.debug('Fetch url: %s.' % server.serverdomain) self.asyncfm.fetch_asynchronously( url, deadline=10, callback=self.processserver, cb_args=[server], headers={'Cache-Control': 'max-age=30'}) def processserver(self, rpc, server): try: response = rpc.get_result() except DownloadError, e: if server.falsepositivecheck: logging.error('Download error. Check the url.') self.serverisdown(server, 000) else: server.falsepositivecheck = True server.put() else: