1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
|
import sqlite3,json,socket,os import sys from datetime import datetime from elasticsearch import Elasticsearch from kafka import KafkaProducer import configparser
def getVariable(): global host1 ,host2 ,host3 ,kafkaPort, kafka1, kafka2, kafka3, indexName, indexName2, dbPath host1 = str(config.get('variable', 'host1')) host2 = str(config.get('variable', 'host2')) host3 = str(config.get('variable', 'host3')) kafkaPort = config.get('variable', 'kafkaPort') kafka1 = config.get('variable', 'kafka1') + ":" + kafkaPort kafka2 = config.get('variable', 'kafka2') + ":" + kafkaPort kafka3 = config.get('variable', 'kafka3') + ":" + kafkaPort indexName = str(config.get('variable', 'indexName')) + "-" + str(datetime.now().year) + "." + str(datetime.now().month) indexName2 = str(config.get('variable', 'indexName2')) + "-" + str(datetime.now().year) + "." + str(datetime.now().month)
dbPath=str(config.get('variable', 'dbPath'))
print("\n[variable]") print("host1="+host1+"\n"+"host2="+host2+"\n"+"host3"+host3) print("kafkaPort="+kafkaPort+"\n"+"kafka1="+kafka1+"\n"+"kafka2="+kafka2+"\n"+"kafka3="+kafka3) print("indexName="+indexName+"\n"+"indexName="+indexName+"") print("dbPath="+dbPath+"\n")
def getDefault():
global port, user, passwd port = str(config.get('default', 'port')).strip('"') user = str(config.get('default', 'user')).strip('"') passwd = str(config.get('default', 'passwd')).strip('"') print("[default]") print("port="+port+"\n"+"user="+user+"\n"+"passwd="+passwd+"\n")
def get_host_ip(): try: s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect(('8.8.8.8', 80)) ip = s.getsockname()[0] finally: s.close() return ip
def indicesExists(my_index): res = es.indices.exists(my_index) return res
def createIndex(my_index): res = es.indices.create(index=my_index, ignore=400) return res
def main(): reload(sys) sys.setdefaultencoding('utf-8')
global IP IP=get_host_ip()
if __name__ == '__main__': main()
global config
config = configparser.ConfigParser()
filename = 'config.ini' config.read(filename, encoding='utf-8')
getVariable()
getDefault()
es = Elasticsearch( [ {"host": host1, "port": port}, {"host": host2, "port": port}, {"host": host3, "port": port} ], http_auth=(user, passwd), timeout=3600 )
producer = KafkaProducer( bootstrap_servers=[kafka1,kafka2,kafka3], value_serializer=lambda v: json.dumps(v).encode('utf-8') )
indexEx=indicesExists(indexName) if indexEx != True: createIndex(indexName)
conn = sqlite3.connect(dbPath) c = conn.cursor() cursor = c.execute("SELECT * from SysLogDebug")
for row in cursor: data = dict([("ip",IP),("logid",str(row[0])),("date",str(row[1])),("assemblyname",str(row[2])),("namespacename",str(row[3])),("classname",str(row[4])),("methodname",str(row[5])),("elapsedtime",str(row[6])),("message",str(row[7]))]) logidEx=es.search(index=indexName, doc_type='_doc', body={"query": {"match":{"logid": row[0]}}}) if str(logidEx).find("logid") == -1: producer.send("syslogdebug", data)
indexEx=indicesExists(indexName2) if indexEx != True: createIndex(indexName2)
cursor2 = c.execute("SELECT * from SysLogError")
for row in cursor2: data2 = dict([("ip",IP),("logid",str(row[0])),("date",str(row[1])),("message",str(row[2]))]) logidEx2=es.search(index=indexName2, doc_type='_doc', body={"query": {"match":{"logid": row[0]}}}) if str(logidEx2).find("logid") == -1: producer.send("syslogerror", data2)
producer.close() conn.close()
exit
|