def upload_file(self, ssh, source, target): ftp = ssh.open_sftp() try: ftp.put(source, target) except Exception as ex: Mu.log_error(self.__logger, "Upload file failed with error: {0}".format(ex)) finally: ftp.close()
def close_ssh_connection(self, ssh): if ssh is not None: try: ssh.close() except Exception as ex: server_name = "" if ssh is not None and hasattr(ssh, "remote_server_name_"): server_name = " on server:{0}".format(ssh.remote_server_name_) Mu.log_error(self.__logger, "Failed to close SSH connection with error:{0}{1}.".format(ex, server_name))
def delivery(self, info_list, info_type, server_id): try: # send heartbeat data self.__producer.send( self.__topic_heartbeat, { Mc.FIELD_SERVER_ID: server_id, Mc.MSG_TYPE: info_type, Mc.MSG_TIME: Mu.generate_check_id() }) self.__producer.flush() if info_list: # as producer doesn't support transaction # https://github.com/dpkp/kafka-python/issues/1396 # https://github.com/dpkp/kafka-python/issues/1063 # add message begin and end part, consumer will abandon all messages if missing begin or end # header Mu.log_debug( self.__logger, "Sending {0} message header to queue...".format(info_type)) self.__producer.send( self.__topic, MsgProducerService.__get_message_header( info_type, server_id)) # body for info in info_list: # for all messages, add type and server id info[Mc.MSG_TYPE] = info_type info[Mc.FIELD_SERVER_ID] = server_id Mu.log_debug( self.__logger, "Sending {0} info {1} to queue...".format( info_type, info)) self.__producer.send(self.__topic, info) Mu.log_debug( self.__logger, "{0} info {1} is sent to queue...".format( info_type, info)) # ending Mu.log_debug( self.__logger, "Sending {0} message ending to queue...".format(info_type)) self.__producer.send( self.__topic, MsgProducerService.__get_message_ending( info_type, server_id)) self.__producer.flush() Mu.log_debug( self.__logger, "Sending {0} message to queue is finished...".format( info_type)) except Exception as ex: Mu.log_error( self.__logger, "Some thing wrong when delivering, error: {0}".format(ex))
def __query_insert_batch(self, query, param_list): cursor = None try: cursor = self.connection.cursor() Mu.log_debug(self.__logger, "query:{0}, param:{1}".format(query, param_list)) cursor.executemany(query, param_list) except Exception as ex: Mu.log_error(self.__logger, "Query:{0} failed with error:{1}".format(query, ex)) Mu.log_exception(self.__logger, traceback.format_exc()) finally: if cursor is not None: cursor.close() Mu.log_debug(self.__logger, "Cursor closed.")
def __init__(self, server_name, port, user, password): # get logging self.__logger = Mu.get_logger(Mc.LOGGER_HANA_DB) # get database connection try: Mu.log_info(self.__logger, "Connecting {0}:{1} with {2}".format(server_name, port, user)) self.connection = dbapi.connect(server_name, port, user, password) if self.connection is None: Mu.log_error(self.__logger, "Connect to HANA error!") else: Mu.log_info(self.__logger, "Connected {0}:{1} with {2}".format(server_name, port, user)) except Exception as ex: error_message = "Connect to HANA error:{0}".format(ex) Mu.log_error(self.__logger, error_message) Mu.log_exception(self.__logger, traceback.format_exc()) raise MonitorDBError(error_message)
def __query_select(self, query): cursor = None try: cursor = self.connection.cursor() self.__logger.debug("query:{0}".format(query)) cursor.execute(query) ret = cursor.fetchall() Mu.log_debug(self.__logger, "Result record count {0}".format(len(ret))) return ret except Exception as ex: Mu.log_error(self.__logger, "Query:{0} failed with error:{1}".format(query, ex)) Mu.log_exception(self.__logger, traceback.format_exc()) return [] finally: if cursor is not None: cursor.close() Mu.log_debug(self.__logger, "Cursor closed.")
def open_ssh_connection(self, server_name, user_name, user_password): ssh = paramiko.SSHClient() try: ssh.load_system_host_keys() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # attach customized attribute for server name, # because there is no official way to get the remote server name later. ssh.remote_server_name_ = server_name ssh.connect(server_name, username=user_name, password=user_password) except Exception as ex: if ssh is not None: ssh.close() Mu.log_error(self.__logger, "SSH connection error:{0}! (Server:{1}, User:{2})".format(ex, server_name, user_name)) # Mu.log_exception(traceback.format_exc()) ssh = None return ssh
def handle_pid_file(self): """ 1. check whether there is agent process running, if yes, just kill the previous process 2. update pid file with current process id at the beginning 3. delete pid file before exit """ pid_file = self.PID_FILE locker = self.LOCKER_FILE # get pid for current process pid = str(os.getpid()) # get current user name user_name = getpass.getuser() Mu.log_info( self.__logger, "Current PID is {0}, trying to check the pid file.".format(pid)) # use file locker for different file (LOCKER_FILE) instead of the pid file, because pid file will be deleted # when the previous agent quit with Mu.open_file_with_lock(locker, "w"): # pid file exists if os.path.isfile(pid_file): Mu.log_info( self.__logger, "{0} already exists, trying to kill the previous agent". format(pid_file)) with open(pid_file) as f: old_pid = f.read() Mu.log_info( self.__logger, "checking the pid {0} of the previous agent".format( old_pid)) # get the process list owned by current user, # started via python and have the same pid as the pid file processes = subprocess.getoutput( "ps -fu {0} | grep '[Pp]ython' | grep {1}".format( user_name, old_pid)) # retry times count = 0 while count < 10 and old_pid and old_pid.strip( ) and processes and processes.strip(): Mu.log_info( self.__logger, "trying to kill the pid {0}.".format(old_pid)) try: os.kill(int(old_pid), signal.SIGKILL) except ProcessLookupError as ex: Mu.log_warning( self.__logger, "Failed to kill the old agent (pid:{0}), error: {1}" .format(old_pid, ex)) processes = subprocess.getoutput( "ps -fu {0} | grep '[Pp]ython' | grep {1}".format( user_name, old_pid)) count += 1 if count >= 10: Mu.log_error( self.__logger, "Some thing wrong happened, can't kill the previous process {0}" .format(old_pid)) raise MonitorOSOpError( "Some thing wrong happens, can't kill the previous process {0}" .format(old_pid)) # write current pid to pid file Mu.log_info( self.__logger, "trying to write the pid file {0} with pid {1}.".format( pid_file, pid)) with open(pid_file, "w") as f: f.write(pid)