init eACGM
This commit is contained in:
29
grafana/src/ollamanet/connect.py
Normal file
29
grafana/src/ollamanet/connect.py
Normal file
@@ -0,0 +1,29 @@
|
||||
# connect to mysql database
|
||||
import mysql.connector
|
||||
|
||||
class database:
|
||||
def __init__(self, ip, port, user, pwd, database) -> None:
|
||||
self.conn = mysql.connector.connect(
|
||||
host = ip,
|
||||
port = port,
|
||||
user = user,
|
||||
password = pwd,
|
||||
database = database
|
||||
)
|
||||
self.cursor = self.conn.cursor()
|
||||
def exec(self, cmd: str):
|
||||
self.cursor.execute(cmd)
|
||||
result = self.cursor.fetchall()
|
||||
self.conn.commit()
|
||||
return result
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
db = database(
|
||||
ip="127.0.0.1",
|
||||
port=3306,
|
||||
user="node1",
|
||||
pwd="mysql114514",
|
||||
database="grafana",
|
||||
)
|
13
grafana/src/ollamanet/init_database.sql
Normal file
13
grafana/src/ollamanet/init_database.sql
Normal file
@@ -0,0 +1,13 @@
|
||||
DROP Table IF EXISTS grafana.ollamanet;
|
||||
DROP TABLE if EXISTS grafana.ipport;
|
||||
CREATE TABLE IF NOT EXISTS grafana.ollamanet
|
||||
(
|
||||
time DATETIME,
|
||||
request DOUBLE,
|
||||
token DOUBLE
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS grafana.ipport
|
||||
(
|
||||
ipport CHAR(255) PRIMARY KEY,
|
||||
cnt INT
|
||||
);
|
37
grafana/src/ollamanet/listen.sh
Normal file
37
grafana/src/ollamanet/listen.sh
Normal file
@@ -0,0 +1,37 @@
|
||||
#!/bin/bash
|
||||
|
||||
sudo tcpdump -i any port 11434 -n -l | awk '
|
||||
BEGIN {
|
||||
start_time = systime()
|
||||
packets = 0
|
||||
inp = 0
|
||||
out = 0
|
||||
}
|
||||
{
|
||||
if ($3 == "In"){
|
||||
inp++
|
||||
}else{
|
||||
out++
|
||||
}
|
||||
packets++
|
||||
current_time = systime()
|
||||
table[$5]++
|
||||
dest = substr($7, 1, length($7) - 1)
|
||||
table[dest]++
|
||||
if (current_time - start_time >= 5) {
|
||||
start_time = current_time
|
||||
timestamp = strftime("%Y-%m-%d %H:%M:%S", current_time)
|
||||
print current_time, packets >> "trace.txt"
|
||||
print inp, out >> "trace.txt"
|
||||
|
||||
for (i in table) {
|
||||
print i, table[i] >> "trace.txt"
|
||||
}
|
||||
print "---" >> "trace.txt"
|
||||
fflush("trace.txt")
|
||||
packets = 0
|
||||
inp=0
|
||||
out=0
|
||||
delete table
|
||||
}
|
||||
}'
|
3
grafana/src/ollamanet/makefile
Normal file
3
grafana/src/ollamanet/makefile
Normal file
@@ -0,0 +1,3 @@
|
||||
run:
|
||||
nohup ./listen.sh > log/listen.log 2>&1 &
|
||||
nohup python tailf.py > log/tailf.log 2>&1 &
|
85
grafana/src/ollamanet/tailf.py
Normal file
85
grafana/src/ollamanet/tailf.py
Normal file
@@ -0,0 +1,85 @@
|
||||
import time
|
||||
import os
|
||||
import argparse
|
||||
from connect import database
|
||||
|
||||
interval = 5
|
||||
max_time = 0
|
||||
|
||||
def tail_f(args, db, filename):
|
||||
with open(filename, 'r') as file:
|
||||
# 移动文件指针到文件末尾
|
||||
file.seek(0, 2)
|
||||
global max_time
|
||||
while True:
|
||||
# 读取新行
|
||||
line = file.readline()
|
||||
|
||||
if not line:
|
||||
time.sleep(1) # 如果没有新行,暂停一秒后继续检查
|
||||
ts = int(time.time())
|
||||
if ts - max_time > interval:
|
||||
db.exec(f"""INSERT INTO {args.database}.ollamanet VALUES (NOW(), 0, 0)""")
|
||||
max_time = ts
|
||||
continue
|
||||
|
||||
yield line
|
||||
|
||||
def main(db:database, args):
|
||||
global interval, max_time
|
||||
log_file = args.file
|
||||
interval = args.interval
|
||||
if not os.path.exists(log_file):
|
||||
os.system(f"touch {log_file}")
|
||||
buf = []
|
||||
for line in tail_f(args, db, log_file):
|
||||
line = line.strip()
|
||||
if line.strip() == "---":
|
||||
l0 = buf[0].split(' ')
|
||||
ts = int(l0[0])
|
||||
max_time = max(max_time, ts)
|
||||
cnt = int(l0[1]) / interval
|
||||
l1 = buf[1].split(' ')
|
||||
recv = int(l1[0]) / interval
|
||||
send = int(l1[1]) / interval
|
||||
# print(f"{ts} {cnt} {recv} {send}")
|
||||
# print(buf)
|
||||
db.exec(f"""INSERT INTO {args.database}.ollamanet VALUES (NOW(), {recv}, {send});""")
|
||||
i = 2
|
||||
while i < len(buf) - 1:
|
||||
l = buf[i].split(' ')
|
||||
ipport = l[0]
|
||||
ipport = ipport[:ipport.rfind('.')]
|
||||
i += 1
|
||||
if ipport == args.local:
|
||||
continue
|
||||
cnt = int(l[1])
|
||||
|
||||
all = db.exec(f"""SELECT cnt from {args.database}.ipport where ipport='{ipport}';""")
|
||||
|
||||
if not all:
|
||||
all = cnt
|
||||
db.exec(f"""INSERT INTO {args.database}.ipport VALUES ('{ipport}', {cnt});""")
|
||||
else:
|
||||
all = all[0][0]
|
||||
all += cnt
|
||||
db.exec(f"""UPDATE {args.database}.ipport SET cnt={all} where ipport='{ipport}';""")
|
||||
buf = []
|
||||
continue
|
||||
buf.append(line)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('--file', type=str, default='trace.txt', help='log file')
|
||||
parser.add_argument('--interval', type=int, default=5, help='interval (s)')
|
||||
parser.add_argument('--ip', type=str, default='127.0.0.1', help='ip')
|
||||
parser.add_argument('--port', type=int, default=3306, help='port')
|
||||
parser.add_argument('--user', type=str, default='node1', help='user')
|
||||
parser.add_argument('--password', type=str, default='mysql114514', help='password')
|
||||
parser.add_argument("--database", type=str, default="grafana", help="database")
|
||||
parser.add_argument("--local", type=str, default="127.0.0.1.11434")
|
||||
args = parser.parse_args()
|
||||
db = database(args.ip, args.port, args.user, args.password, args.database)
|
||||
main(db, args)
|
||||
|
60
grafana/src/ollamanet/trace.txt
Normal file
60
grafana/src/ollamanet/trace.txt
Normal file
@@ -0,0 +1,60 @@
|
||||
1722135153 93
|
||||
34 59
|
||||
100.77.22.47.18099 6
|
||||
100.77.22.47.18188 87
|
||||
100.82.183.119.11434 93
|
||||
---
|
||||
1722135153 93
|
||||
34 59
|
||||
100.77.22.47.18099 1
|
||||
100.77.22.47.18188 92
|
||||
100.82.183.119.11434 93
|
||||
---
|
||||
1722135155 93
|
||||
33 60
|
||||
100.77.22.47.18188 93
|
||||
100.82.183.119.11434 93
|
||||
---
|
||||
1722135155 1
|
||||
1 0
|
||||
100.82.183.119.11434 1
|
||||
100.77.22.47.17946 1
|
||||
---
|
||||
1722135230 62
|
||||
23 39
|
||||
100.82.183.119.11434 62
|
||||
100.77.22.47.17946 62
|
||||
---
|
||||
1722135230 93
|
||||
36 57
|
||||
100.77.22.47.18188 19
|
||||
100.82.183.119.11434 93
|
||||
100.77.22.47.17946 74
|
||||
---
|
||||
1722135230 93
|
||||
35 58
|
||||
100.77.22.47.18188 14
|
||||
100.82.183.119.11434 93
|
||||
100.77.22.47.17946 79
|
||||
---
|
||||
1722135231 94
|
||||
35 59
|
||||
100.77.22.47.18188 12
|
||||
100.82.183.119.11434 94
|
||||
100.77.22.47.17946 82
|
||||
---
|
||||
1722137005 1
|
||||
1 0
|
||||
100.82.183.119.11434 1
|
||||
100.77.22.47.8112 1
|
||||
---
|
||||
1722137235 126
|
||||
44 82
|
||||
100.82.183.119.11434 126
|
||||
100.77.22.47.8112 126
|
||||
---
|
||||
1722137236 1
|
||||
1 0
|
||||
100.82.183.119.11434 1
|
||||
100.77.22.47.55880 1
|
||||
---
|
Reference in New Issue
Block a user