arthas、redis、aes、rsa、zk、kafka
阿新 • • 發佈:2020-11-20
1、下載
wgethttp://10.177.240.232:8000/arthasInstall.sh
2、安裝
bash arthasInstall.sh
3、使用
切換到service使用者,
到/home/service/app/*/*/release 的任意工程目錄下
執行:arthas
獲得jar包原始碼,儲存路徑 release/arthas-output路徑下
執行source /etc/profile或者切換一下使用者
4、修改
cdrelease/arthas-output/com/* 到原始碼的路徑下,
vim 檔名檢視內容,並修改
5、熱更虛擬機器
update filename.java
其他說明:
熱更檔案可能會失敗,原因可能:
1、修改之後語法問題,
2、java檔案本身沒有載入
3、編譯環境異常等
aes加密 採用CTR方式:
class AESCipher:
def __init__(self, key):
# self.key = key.decode("base64")
self.key = key
print key
def encrypt(self, raw, iv='1234567890123456'):
"""
Returns hex encoded encrypted value! MODE_ECB
"""
BS = 16
# pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS) # 對要加密的內容按16位進行補位
# iv = Random.new().read(AES.block_size)
iv = iv
# counter = Counter.new(128, initial_value=bytes_to_long(iv))
ctr_e = Crypto.Util.Counter.new(128, initial_value=long(iv.encode('hex'), 16))
# cipher = AES.new(self.key, AES.MODE_ECB, iv)
cipher = AES.new(self.key, AES.MODE_CTR, counter=ctr_e)
# raw = pad(raw)
return base64.b64encode(str(cipher.encrypt(raw)))
# return str(cipher.encrypt(raw)).encode("base64").strip()
def decrypt(self, enc, iv='1234567890123456'):
"""
Requires hex encoded param to decrypt
"""
# enc = urllib.unquote(enc).decode('utf-8') # 特殊字元(+ = ..)轉換一下
enc = enc.decode("base64")
# iv = enc[:16]
iv = iv
# cipher = AES.new(self.key, AES.MODE_ECB, iv)
ctr_e = Crypto.Util.Counter.new(128, initial_value=long(iv.encode('hex'), 16))
# ctr_e = Crypto.Util.Counter.new(128, initial_value=0)
cipher = AES.new(self.key, AES.MODE_CTR, counter=ctr_e)
dec_str = cipher.decrypt(enc).strip('\x10')
return dec_str
redis 資料查詢 和kafka 資料寫入
import base64
import hashlib
import time
from rediscluster import StrictRedisCluster
proxies = {'http': 'http://localhost:8888', 'https': 'http://localhost:8888'}
def redis_test():
startup_nodes = [
{"host": "10.177.89.80", "port": 6379}, # 主
{"host": "10.177.89.81", "port": 6379}, # 6379的從資料庫
{"host": "10.177.89.79", "port": 6379}, #
{"host": "10.177.89.82", "port": 6379}, #
]
redis_store = StrictRedisCluster(startup_nodes=startup_nodes, decode_responses=True)
redis_store.set('name', '老鷹')
redis_store.set('money', '10億')
print "查詢到的值為: {}".format(redis_store.smembers('next_config_key'))
from pykafka import KafkaClient
class operateKafka:
def __init__(self):
myhosts = "10.177.201.80:9092,10.177.201.81:9092,10.177.201.82:9092"
client = KafkaClient(hosts=myhosts) # 可接受多個Client這是重點
self.topic = client.topics['sms_imei_map'] # 選擇一個topic
def sendMessage(self, dataList):
with self.topic.get_sync_producer() as producer:
producer.produce((str(dataList)).encode())
RSA加密 公鑰 私鑰方式 對應java OAEPPadding方式
# coding=utf-8
# -*- coding: utf-8 -*-
import base64
import os
from M2Crypto import BIO, RSA
report_folder = os.path.normpath(os.path.join(os.path.dirname(os.path.dirname(__file__)),'common'))
with open(report_folder+"/rsa_public.pem", 'r') as f:
pubkey = f.read()
with open(report_folder+"/rsa_private_pkcs8.pem", 'r') as f:
prikey = f.read()
def sms_encrypt(message):
# 加密
pub_bio = BIO.MemoryBuffer(pubkey.encode('utf-8')) # 公鑰字串
pub_rsa = RSA.load_pub_key_bio(pub_bio) # 載入公鑰
secret = pub_rsa.public_encrypt(message, RSA.pkcs1_oaep_padding) # 公鑰加密 RSA/NONE/OAEPPadding
sign = base64.b64encode(secret) # 密文base64編碼
return sign
# 解密
def sms_decrypt(b64_sign):
cipher = base64.b64decode(b64_sign) # base64解碼
pri_bio = BIO.MemoryBuffer(prikey.encode('utf-8')) # 載入私鑰
pri_rsa = RSA.load_key_bio(pri_bio)
plain = pri_rsa.private_decrypt(cipher, RSA.pkcs1_oaep_padding) # 解密
return plain
zk資料查詢和更新
# -*- coding:utf-8 -*-
import sys
import logging
from kazoo.client import KazooClient
import zk_setting
sys.path.append('../')
logger = logging.getLogger('ats.zk.common')
class ZkClient(object):
def __init__(self, host, port, timeout):
try:
zk = KazooClient(hosts=host+':'+port, timeout=timeout)
zk.start() # 啟動zk連線
self.zk = zk
logger.debug('connect success, host:{0}, port:{1}'.format(host, port))
print 'connect success, host:{0}, port:{1}'.format(host, port)
except Exception as e:
logger.error("connect zk failed -- host:%s, port:%s, error:%s" % (host, port, str(e)))
def get_nodes(self, path):
"""獲取path下的節點"""
if self.zk.exists(path):
try:
result = self.zk.get_children(path)
return result
except Exception as e:
self.close_conn()
raise ValueError("get_nodes falied: path -- '%s' , error -- %s" % (path, str(e)))
else:
raise ValueError("node path - '%s' not exist." % path)
def get_children_nodes(self, path):
if self.zk.exists(path):
try:
result = self.zk.get_children(path)
return result
except Exception as e:
# self.close_conn()
raise ValueError("get_nodes_content falied: path -- '%s' , error -- %s" % (path, str(e)))
else:
raise ValueError("node path - '%s' not exist." % path)
def get_nodes_content(self, path):
"""獲取path節點下的值"""
if self.zk.exists(path):
try:
result = self.zk.get(path)
return result[0]
except Exception as e:
# self.close_conn()
logger.error("get_nodes_content falied: path -- '%s' , error -- %s" % (path, str(e)))
return False
else:
logger.warn("node path - '%s' not exist." % path)
return False
def set_nodes(self, path, value=b''):
"""設定節點值,如果節點不存在則新建該節點"""
if self.zk.exists(path):
try:
result = self.zk.set(path, str(value))
logger.debug("set node success -- node path:'%s', value:%s, report:%s" % (str(path), str(value), str(result)))
return True
except Exception as e:
self.close_conn()
logger.error("set_nodes falied: path -- '%s' , error -- %s" % (path, str(e)))
return False
else:
print "node '%s' is not exist, now it will create." % path
try:
create_zk = self.zk.create(path, str(value))
logger.debug("create node success -- path:'%s', value:%s, report:%s" % (str(path), str(value), str(create_zk)))
result = self.zk.set(path, str(value))
logger.debug("set node success -- node path:'%s', value:%s, report:%s" % (str(path), str(value), str(result)))
return True
except Exception as e:
self.close_conn()
logger.error("set_nodes falied: path -- '%s' , error -- %s" % (path, str(e)))
return False
# return report
def delete_node(self, path):
if self.zk.exists(path):
try:
del_r = self.zk.delete(path)
print "delete node success -- node path:'%s'" % str(path)
except Exception as e:
self.close_conn()
raise ValueError("delete_node falied: path -- '%s' , error -- %s" % (path, str(e)))
else:
raise ValueError("node path - '%s' not exist." % path)
def is_the_latest_node(self, path):
"""
判斷當前節點路徑下的下一級節點是否是最後一級節點
:param path: 當前節點路徑
:return:
"""
if self.zk.exists(path):
try:
if path == '/':
return 'N'
else:
children_nodes = self.get_children_nodes(path=path)
if len(children_nodes) > 0:
next_node = path + '/' + children_nodes[0]
next_node_list = self.get_children_nodes(path=next_node)
if len(next_node_list) > 0:
return 'N'
else:
return 'Y'
else:
return 'Y'
except Exception as e:
self.close_conn()
logger.error("get_nodes_content falied: path -- '%s' , error -- %s" % (path, str(e)))
return 'E'
else:
logger.warn("node path - '%s' not exist." % path)
return 'E'
def close_conn(self):
self.zk.stop()
def test_zk(cmd='ls', path='/', value=b'', host=zk_setting.zk_host, port=zk_setting.zk_port, timeout=5.0):
zk_cli = ZkClient(host, port, timeout)
if cmd == 'ls':
zk_r = zk_cli.get_nodes(path)
elif cmd == 'get':
# zk_cli.zk.get(path=path)
logger.info("******get -- path={0}".format(path))
zk_r = zk_cli.get_nodes_content(path)
elif cmd == 'set':
if value:
zk_r = zk_cli.set_nodes(path, value=value)
else:
print 'set value is none...'
raise ValueError('set value is none')
elif cmd == 'delete':
zk_r = zk_cli.delete_node(path)
elif cmd == 'get_childern':
zk_r = zk_cli.get_children_nodes(path)
else:
zk_cli.close_conn()
raise ValueError("command error, cmd must in [ls, get, set, delete]")
zk_cli.close_conn()
return zk_r
def zk_node_traversing(node='/'):
zk_client = ZkClient(host=zk_setting.zk_host, port=zk_setting.zk_port, timeout=5.0)
latest_node_flag = zk_client.is_the_latest_node(path=node)
result = {}
if latest_node_flag == 'Y':
nodes = zk_client.get_children_nodes(path=node)
node_data_list = []
for n in nodes:
# print n
latest_node = node + '/' + n
node_info = {
'node_path': node,
'node': n,
'node_full_path': latest_node,
'node_data': zk_client.get_nodes_content(path=latest_node)
}
node_data_list.append(node_info)
result['node_data_list'] = node_data_list
result['ret'] = 2
elif latest_node_flag == 'N':
result['node_path_list'] = zk_client.get_children_nodes(path=node)
result['ret'] = 1
else:
result = {
'ret': -1,
'msg': 'the node "{0}" not exist.'.format(node)
}
# print json.dumps(report)
zk_client.close_conn()
zk_client.close_conn()
return result
def get_file_cfg(filename, warn_add=False):
cfg_list = []
with open(name=filename, mode='r') as f:
i = 1
j = 1
for line in f.readlines():
if line.strip() == '':
print i
print '============================='
if line.strip():
split_data = line.split('=')
if len(split_data) == 2:
field = line.split('=')[0].strip()
value = line.split('=')[1].strip().replace(' ', '')
# print '{0}--field={1}--value={2}'.format(i, field, value)
cfg_list.append({"field": field, "value": value})
i += 1
j += 1
else:
# print '************the {0} line data may be error:{1}'.format(j, line)
if warn_add:
field = line[0:line.find("=")]
value = line[line.find("=")+1:]
cfg_list.append({"field": field, "value": value})
# print 'inclue "=" :{0}--field={1}--value={2}'.format(i, field, value)
i += 1
j += 1
else:
j += 1
return cfg_list
def import_cfg(module_node):
cfg_list = get_file_cfg(filename='feeds.properties', warn_add=True)
zk_client = ZkClient(host=zk_setting.zk_host, port=zk_setting.zk_port, timeout=5.0)
i = 0
is_module_node_exist = zk_client.zk.exists(path=module_node)
if is_module_node_exist:
for cfg in cfg_list:
dst_node_path = module_node.strip('/') + '/' +cfg.get('field')
cur_node_data = str(zk_client.get_nodes_content(path=dst_node_path)).replace(' ', '')
if cur_node_data != cfg.get('value'):
# set_ret = zk_client.set_nodes(path=dst_node_path, value=cfg.get('value'))
set_ret = False
if set_ret:
print dst_node_path
print '{2}_cur_data={0}\n{2}_new_data={1}'.format(cur_node_data, cfg.get('value'), cfg.get('field'))
i += 1
else:
print 'path:{0} - set failed'.format(dst_node_path)
print 'total nums:', i
else:
print 'module node "{0}" not exist'.format(module_node)