示例#1
0
文件: BotApi.py 项目: junoteam/pybot
 def get_automatic_site_status(self):
     status_huyatus = SystemUtils()
     while 1:
         try:
             status_huyatus.get_automatic_site_status()
             time.sleep(5)
         except Exception, e:
              time.sleep(5)
              print str(e)
 def which(self,mess,args):
     ''' UNIX which command '''
     path=SystemUtils.which(args)
     if path:
         return "%s : %s" % (args,path)
     else:
         return "%s : command not found" % args
 def logs(self,mess,args):
     __ll=' | '.join( self.__logs.keys() )
     usage="usage:\n logs < tail | head > [< #lines >] < %s >" % __ll + "\nor\n\t logs cat < %s >" % ' | '.join( self.__logs.keys() )
     usage_cat="usage:\n logs cat < %s >" % __ll
     usage_tail="usage:\n logs tail [<#lines>] < %s >" % __ll
     usage_head="usage:\n logs head [<#lines>] < %s >" % __ll
     
     """ show log files """
     if self.__logs != None:
         try:
             argz = self.__splitstr(args)
             
             ''' cat '''
             if argz[0] == 'cat':
                 if self.__logs.has_key(argz[1]) :
                     return SystemUtils.cat(self.__logs[argz[1]])
                 else:
                     return usage_cat
             
             ''' tail '''
             if argz[0] == 'tail':
                 if argz[1].isdigit():
                     if self.__logs.has_key(argz[2]) :
                         return SystemUtils.tail(self.__logs[argz[2]],int(argz[1]))
                     return SystemUtils.tail(self.__logs[argz[2]])
                 if self.__logs.has_key(argz[1]) :
                     return SystemUtils.tail(self.__logs[argz[1]])
                 
                 return usage_tail
             
             ''' head '''
             if argz[0] == 'head':
                 if argz[1].isdigit():
                     if self.__logs.has_key(argz[2]) :
                         return SystemUtils.tail(self.__logs[argz[2]],int(argz[1]))
                     return SystemUtils.head(self.__logs[argz[2]])
                 if self.__logs.has_key(argz[1]) :
                     return SystemUtils.head(self.__logs[argz[1]])
                 return usage_head
             
         except IndexError:
             return usage
         except Exception as e:
             return "Exception occurred < %s >" % e.message
     else:
         return "log files not set in configuration file"
     
     return usage
 def unknown_command(self, mess, cmd, args):
     path=SystemUtils.which(cmd)
     if path is not None:
         return self.__sysu.execmd(str(mess.getBody()))
     else:
         return None
 def __init__(self, username, password,auth_users):
     jabberbot.JabberBot.__init__(self, username, password)
     self.auth_users=auth_users
     self.sysinfo = SystemInfo()
     self.__sysu= SystemUtils()
示例#6
0
文件: BotApi.py 项目: junoteam/pybot
    def get_command(self):
        path_to_file = os.getcwd()
        file_with_id = path_to_file + "/update_id_file.txt"

        with open(file_with_id, 'r') as myfile:
            last_id = myfile.read()
            myfile.close()

        offset = int(last_id)
        offset = int(offset) - 1

        values = {}
        values['offset'] = offset
        values['limit'] = '5'

        data = urllib.urlencode(values)
        response = urllib2.Request(self.update_url, data)
        response = urllib2.urlopen(response)
        block_from_json_response = response.read()

        self.json_response = json.loads(block_from_json_response)
        last_update_id_from_json = self.json_response['result'][-1]['update_id']
        last_user_id = self.json_response['result'][-1]['message']['from']['id']

        update_id_array = []
        for key in self.json_response['result']:
            update_id_array.append(key['update_id'])

        path_to_file = os.getcwd()
        file_with_id = path_to_file + "/update_id_file.txt"

        if os.path.exists(file_with_id):
            with open(file_with_id, 'r+') as myfile:
                last_update_id = myfile.read()

                if int(last_update_id) == int(last_update_id_from_json):
                    myfile.close()
                else:
                    myfile.read()
                    myfile.seek(0)
                    myfile.write(str(last_update_id_from_json))
                    myfile.close()

                    command = self.json_response['result'][-1]['message']['text']
                    system = SystemUtils()

                    if last_user_id == self.user_id:
                        if command == BotApi.info_command:
                            inf = system.get_info_from_server(command)
                            information = inf.to_string()
                            self.send_message(command, information)
                        elif command == BotApi.help_command:
                            self.send_message(command, '')
                        elif command == BotApi.load_command:
                            load = system.get_load_from_from_server(command)
                            self.send_message(command, load)
                        elif command == BotApi.mem_commmand:
                            mem = system.get_memory(command)
                            self.send_message(command, mem)
                        elif command == BotApi.apache_restart:
                            restart = system.restart_apache(command)
                            self.send_message(command, restart)
                        elif command == BotApi.mysql_restart:
                            restart = system.restart_mysqld(command)
                            self.send_message(command, restart)
                        elif command == BotApi.check_site_status:
                            online_status = system.get_status_online(command)
                            self.send_message(command, online_status)
                        else:
                            self.send_message(command, '')
                            print Bcolors.OKGREEN + "Nothing to send, error command has been entered" + Bcolors.ENDC
                    else:
                        self.detect_wrong_user()
        else:
            print "File: " + file_with_id + " doesn't exist"
示例#7
0
    def get_command(self):
        path_to_file = os.getcwd()
        file_with_id = path_to_file + "/update_id_file.txt"

        with open(file_with_id, 'r') as myfile:
            last_id = myfile.read()
            myfile.close()

        offset = int(last_id)
        offset = int(offset) - 1

        values = {}
        values['offset'] = offset
        values['limit'] = '5'

        data = urllib.urlencode(values)
        response = urllib2.Request(self.update_url, data)
        response = urllib2.urlopen(response)
        block_from_json_response = response.read()

        self.json_response = json.loads(block_from_json_response)
        last_update_id_from_json = self.json_response['result'][-1][
            'update_id']
        last_user_id = self.json_response['result'][-1]['message']['from'][
            'id']

        update_id_array = []
        for key in self.json_response['result']:
            update_id_array.append(key['update_id'])

        path_to_file = os.getcwd()
        file_with_id = path_to_file + "/update_id_file.txt"

        if os.path.exists(file_with_id):
            with open(file_with_id, 'r+') as myfile:
                last_update_id = myfile.read()

                if int(last_update_id) == int(last_update_id_from_json):
                    myfile.close()
                else:
                    myfile.read()
                    myfile.seek(0)
                    myfile.write(str(last_update_id_from_json))
                    myfile.close()

                    command = self.json_response['result'][-1]['message'][
                        'text']
                    system = SystemUtils()

                    if last_user_id == self.user_id:
                        if command == BotApi.info_command:
                            inf = system.get_info_server(command)
                            information = inf.to_string()
                            self.send_message(command, information)
                        elif command == BotApi.help_command:
                            self.send_message(command, '')
                        elif command == BotApi.load_command:
                            load = system.get_load_server(command)
                            self.send_message(command, load)
                        elif command == BotApi.mem_command:
                            mem = system.get_mem_server(command)
                            self.send_message(command, mem)
                        else:
                            self.send_message(command, '')
                            print "Nothing to send, error command has been entered"
                    else:
                        print "sad"
        else:
            print "File: " + file_with_id + " doesn't exist"