Пример #1
0
 def post(self, *args, **kwargs):
     logger_dict = {"args": args, "kwargs": kwargs, "params": self.params, "method": "POST"}
     try:
         interface = args[0]
         method_settings = {
             "jx3/info": self.update_jx3_info
         }
         response = yield method_settings[interface]()
         self.write(response)
         self.finish()
     except Exception, e:
         logger_dict["traceback"] = traceback.format_exc(e)
         logger.error(logger_dict)
         send_to_master("API出错", json.dumps(logger_dict))
         self.write({"status": 0, "message": "获取失败"})
Пример #2
0
 def get(self, *args, **kwargs):
     logger_dict = {"args": args, "kwargs": kwargs, "params": self.params, "method": "POST"}
     interface = args[0]
     method_settings = {
         "jx3/info": self.get_jx3info
     }
     if interface not in method_settings:
         raise HTTPError(404)
     try:
         response = yield method_settings[interface]()
         self.write(response)
     except Exception, e:
         logger_dict["traceback"] = traceback.format_exc(e)
         logger.error(logger_dict)
         send_to_master("API出错", json.dumps(logger_dict))
         self.write({"status": 0, "message": "获取失败"})
Пример #3
0
 def auto_add_one(cls, force=False):
     last_time = redis.get(cls.LAST_UPDATE_TIME)
     if not force and last_time and not Timer.is_in_yesterday(last_time):
         send_to_master("剑网三自动更新出错", "上次更新时间错误!")
         return False
     with redis.conn.pipeline() as pipe:
         with sessionCM() as session:
             try:
                 pipe.watch(cls.REDIS_KEY, cls.LAST_UPDATE_TIME)
                 pipe.multi()
                 res = redis.incr(cls.REDIS_KEY)
                 if not res:
                     raise RedisError
                 time = Timer.now_time()
                 redis.set(cls.LAST_UPDATE_TIME, Timer.now_time())
                 pipe.execute()
                 current_jx3_info = json.dumps(cls.get_info())
                 Jx3DailyRecord.create(session,
                                       update_time=time,
                                       info=current_jx3_info)
                 send_to_master("剑网三自动更新完成", current_jx3_info)
                 return True
             except WatchError:
                 pipe.reset()
Пример #4
0
 def get_usage(self):
     with paramiko.SSHClient() as ssh:
         record = self.get_record()
         ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
         try:
             ssh.connect(hostname=self.public_ip,
                         port=22,
                         username=record.user_name,
                         password=record.pwd)
             cpu_reset = self.exec_command(
                 ssh,
                 "top -bn 1 -i -c|grep Cpu|awk -F, '{print $4}'| awk -Fi '{print $1}'"
             )
             server_load = self.exec_command(
                 ssh,
                 "uptime | sed 's/.*average://g' | sed 's/\s*//g'| awk -F, '{print $1}'"
             )
             memory_rest = self.exec_command(
                 ssh, "free -m | grep Mem | awk '{print $7}'")
             disk_usage = self.exec_command(
                 ssh, "df -m | awk '{print $3}'| head -n 2 | tail -n 1")
             mysql_cpu = self.exec_command(
                 ssh,
                 "top -b -n 1 | grep mysqld | awk '{b+=$9}END{print b}'")
             mongo_cpu = self.exec_command(
                 ssh,
                 "top -b -n 1 | grep mongod | awk '{b+=$9}END{print b}'")
             UsageRecord.create(
                 self.session, **{
                     "cpu_usage":
                     1 - float(cpu_reset.strip().strip("%")) / 100,
                     "server_load":
                     server_load,
                     "memory_usage":
                     1 - float(float(memory_rest) / record.memory_capacity),
                     "disk_usage":
                     float(float(disk_usage) / record.disk_capacity),
                     "mysql_cpu":
                     float(mysql_cpu or 0),
                     "mongo_cpu":
                     float(mongo_cpu or 0),
                     "robot_id":
                     record.id
                 })
             record.error_times = 0
             record.status = STATUS["ACTIVE"]
             if mysql_cpu and float(mysql_cpu) > LINE["mysql"]:
                 raise OverloadError
             if mongo_cpu and float(mongo_cpu) > LINE["mysql"]:
                 raise OverloadError
         except OverloadError, e:
             send_to_master(
                 "服务器%s负载过高" % record.name,
                 "%s机器数据库负载过高当前机器状态为\n系统负载:%f\nCPU使用率:%f\n内存使用率:%f\n硬盘使用率:%f\nmysql CPU占用:%f\nmongo CPU占用:%f"
                 % (record.name, record.server_load, record.cpu_usage,
                    record.memory_usage, record.disk_usage,
                    record.mysql_cpu, record.mongo_cpu))
         except socket.error, e:
             record.error_times += 1
             record.status = STATUS["CONNECT_ERROR"]
             logger.error("获取状态信息失败:%s" % e.message)
             logger.error(traceback.format_exc(e))
Пример #5
0
            except StdError, e:
                record.error_times += 1
                record.status = STATUS["OTHER_ERROR"]
                logger.error("获取状态信息失败:%s" % e.message)
                logger.error(traceback.format_exc(e))
            except Exception, e:
                self.session.rollback()
                record.error_times += 1
                record.status = STATUS["OTHER_ERROR"]
                print traceback.format_exc(e)
                logger.error("获取状态信息失败:%s" % e.message)
                logger.error(traceback.format_exc(e))
            finally:
                if record.error_times > 5:
                    record.status = STATUS["STOPPED"]
                    send_to_master("API出错",
                                   "%s机器连续五分钟获取不到信息, 设置状态为stop!" % record.name)
                self.session.add(record)
                self.session.commit()

    def add_usage_to_record(self):
        pass

    def exec_command(self, ssh, commannd):
        stdin, stdout, stderr = ssh.exec_command(commannd)
        str_out = stdout.read().decode()
        str_err = stderr.read().decode()
        if str_err != "":
            raise StdError(str_err)
        return str_out.strip()