| 1 |
#!/usr/bin/env python3 |
| 2 |
# ========================================================================== |
| 3 |
# _ _ _ ____ ____ _____ |
| 4 |
# | | | (_) _ \ ___ _ __ / ___|___ _ _|_ _| __ __ _ ___ ___ _ __ |
| 5 |
# | |_| | | |_) / _ \ '__| | / _ \| '_ \| || '__/ _` |/ __/ _ \ '__| |
| 6 |
# | _ | | __/ __/ | | |__| (_) | | | | || | | (_| | (_| __/ | |
| 7 |
# |_| |_|_|_| \___|_| \____\___/|_| |_|_||_| \__,_|\___\___|_| |
| 8 |
# |
| 9 |
# --- High-Performance Connectivity Tracer (HiPerConTracer) --- |
| 10 |
# https://www.nntb.no/~dreibh/hipercontracer/ |
| 11 |
# ========================================================================== |
| 12 |
# |
| 13 |
# High-Performance Connectivity Tracer (HiPerConTracer) |
| 14 |
# Copyright (C) 2015-2026 by Thomas Dreibholz |
| 15 |
# |
| 16 |
# This program is free software: you can redistribute it and/or modify |
| 17 |
# it under the terms of the GNU General Public License as published by |
| 18 |
# the Free Software Foundation, either version 3 of the License, or |
| 19 |
# (at your option) any later version. |
| 20 |
# |
| 21 |
# This program is distributed in the hope that it will be useful, |
| 22 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 24 |
# GNU General Public License for more details. |
| 25 |
# |
| 26 |
# You should have received a copy of the GNU General Public License |
| 27 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 28 |
# |
| 29 |
# Contact: [email protected] |
| 30 |
|
| 31 |
import sys |
| 32 |
import datetime |
| 33 |
import ipaddress |
| 34 |
|
| 35 |
import QueryHelper |
| 36 |
|
| 37 |
|
| 38 |
# ###### Main program ####################################################### |
| 39 |
if len(sys.argv) < 3: |
| 40 |
sys.stderr.write('Usage: ' + sys.argv[0] + ' database_configuration ping|traceroute from_time to_time\n') |
| 41 |
sys.exit(1) |
| 42 |
configurationFile = sys.argv[1] |
| 43 |
queryType = sys.argv[2] |
| 44 |
fromTimeStamp = None |
| 45 |
toTimeStamp = None |
| 46 |
if len(sys.argv) > 3: |
| 47 |
fromTimeStamp = datetime.datetime.strptime(sys.argv[3], "%Y-%m-%d %H:%M:%S") |
| 48 |
if len(sys.argv) > 4: |
| 49 |
toTimeStamp = datetime.datetime.strptime(sys.argv[4], "%Y-%m-%d %H:%M:%S") |
| 50 |
|
| 51 |
# ====== Connect to database ================================================ |
| 52 |
configuration = QueryHelper.DatabaseConfiguration(sys.argv[1]) |
| 53 |
client = configuration.createClient() |
| 54 |
|
| 55 |
|
| 56 |
# ====== Query Ping data ==================================================== |
| 57 |
if queryType == 'ping': |
| 58 |
# ------ SQL: MySQL/MariaDB/PostgreSQL ----------------------------------- |
| 59 |
if configuration.getBackend() in [ 'MySQL', 'MariaDB', 'PostgreSQL' ]: |
| 60 |
query = 'SELECT SendTimestamp,MeasurementID,SourceIP,DestinationIP,Protocol,TrafficClass,BurstSeq,PacketSize,ResponseSize,Checksum,Status,TimeSource,Delay_AppSend,Delay_Queuing, Delay_AppReceive,RTT_App,RTT_SW,RTT_HW FROM Ping ORDER BY Timestamp,MeasurementID,SourceIP,DestinationIP,Protocol,TrafficClass,RoundNumber,HopNumber' |
| 61 |
if fromTimeStamp != None: |
| 62 |
query += ' WHERE TimeStamp >= \'' + str(fromTimeStamp) + '\'' |
| 63 |
if toTimeStamp != None: |
| 64 |
query += ' AND TimeStamp < \'' + str(toTimeStamp) + '\'' |
| 65 |
print(query) |
| 66 |
rows = configuration.query(query) |
| 67 |
|
| 68 |
for row in rows: |
| 69 |
timeStamp = round(row[0].replace(tzinfo=datetime.timezone.utc).timestamp() * 1000000.0) |
| 70 |
sourceIP = QueryHelper.unmap(ipaddress.ip_address(row[1])) |
| 71 |
destinationIP = QueryHelper.unmap(ipaddress.ip_address(row[2])) |
| 72 |
checksum = int(row[3]) |
| 73 |
packetSize = int(row[4]) |
| 74 |
trafficClass = int(row[5]) |
| 75 |
status = int(row[6]) |
| 76 |
rtt = int(row[7]) |
| 77 |
|
| 78 |
sys.stdout.write('#P ' + |
| 79 |
str(sourceIP) + ' ' + |
| 80 |
str(destinationIP) + ' ' + |
| 81 |
hex(timeStamp)[2:] + ' ' + |
| 82 |
hex(checksum)[2:] + ' ' + |
| 83 |
str(status) + ' ' + |
| 84 |
str(rtt) + ' ' + |
| 85 |
hex(trafficClass)[2:] + ' ' + |
| 86 |
str(packetSize) + '\n') |
| 87 |
|
| 88 |
# ------ NoSQL: MongoDB -------------------------------------------------- |
| 89 |
elif configuration.getBackend() == 'MongoDB': |
| 90 |
query = { } |
| 91 |
if fromTimeStamp != None: |
| 92 |
ft = { 'timestamp': { '$gte': str(fromTimeStamp) } } |
| 93 |
if toTimeStamp != None: |
| 94 |
tt = { 'timestamp': { '$lt': str(toTimeStamp) } } |
| 95 |
query = { '$and': [ ft, tt ] } |
| 96 |
else: |
| 97 |
query = ft |
| 98 |
# print(query) |
| 99 |
rows = configuration.queryMongoDB('ping', query) |
| 100 |
|
| 101 |
for row in rows: |
| 102 |
timeStamp = int(row['timestamp']) |
| 103 |
sourceIP = ipaddress.ip_address(bytes(row['source'])) |
| 104 |
destinationIP = ipaddress.ip_address(row['destination']) |
| 105 |
checksum = int(row['checksum']) |
| 106 |
packetSize = int(row['pktsize']) |
| 107 |
trafficClass = int(row['tc']) |
| 108 |
status = int(row['status']) |
| 109 |
rtt = int(row['rtt']) |
| 110 |
sys.stdout.write('#P ' + |
| 111 |
str(sourceIP) + ' ' + |
| 112 |
str(destinationIP) + ' ' + |
| 113 |
hex(timeStamp)[2:] + ' ' + |
| 114 |
hex(checksum)[2:] + ' ' + |
| 115 |
str(status) + ' ' + |
| 116 |
str(rtt) + ' ' + |
| 117 |
hex(trafficClass)[2:] + ' ' + |
| 118 |
str(packetSize) + '\n') |
| 119 |
|
| 120 |
|
| 121 |
# ====== Query Traceroute data ============================================== |
| 122 |
elif queryType == 'traceroute': |
| 123 |
# ------ SQL: MySQL/MariaDB/PostgreSQL ----------------------------------- |
| 124 |
if configuration.getBackend() in [ 'MySQL', 'MariaDB', 'PostgreSQL' ]: |
| 125 |
query = 'SELECT Timestamp,MeasurementID,SourceIP,DestinationIP,Protocol,TrafficClass,RoundNumber,HopNumber,TotalHops,PacketSize,ResponseSize,Checksum,Status,PathHash,SendTimestamp,HopIP,TimeSource,Delay_AppSend,Delay_Queuing,Delay_AppReceive,RTT_App,RTT_SW,RTT_HW FROM Traceroute ORDER BY Timestamp,MeasurementID,SourceIP,DestinationIP,Protocol,TrafficClass,RoundNumber,HopNumber' |
| 126 |
if fromTimeStamp != None: |
| 127 |
query += ' WHERE TimeStamp >= \'' + str(fromTimeStamp) + '\'' |
| 128 |
if toTimeStamp != None: |
| 129 |
query += ' AND TimeStamp < \'' + str(toTimeStamp) + '\'' |
| 130 |
rows = configuration.query(query) |
| 131 |
|
| 132 |
for row in rows: |
| 133 |
timeStamp = round(row[0].replace(tzinfo=datetime.timezone.utc).timestamp() * 1000000.0) |
| 134 |
sourceIP = QueryHelper.unmap(ipaddress.ip_address(row[1])) |
| 135 |
destinationIP = QueryHelper.unmap(ipaddress.ip_address(row[2])) |
| 136 |
roundNumber = int(row[3]) |
| 137 |
checksum = int(row[4]) |
| 138 |
packetSize = int(row[5]) |
| 139 |
trafficClass = int(row[6]) |
| 140 |
hopNumber = int(row[7]) |
| 141 |
totalHops = int(row[8]) |
| 142 |
status = int(row[9]) |
| 143 |
rtt = int(row[10]) |
| 144 |
hopIP = QueryHelper.unmap(ipaddress.ip_address(row[11])) |
| 145 |
pathHash = int(row[12]) |
| 146 |
if pathHash < 0: |
| 147 |
pathHash = 0x10000000000000000 - abs(pathHash) |
| 148 |
|
| 149 |
if hopNumber == 1: |
| 150 |
statusFlags = status - (status & 0xff) |
| 151 |
sys.stdout.write('#T ' + |
| 152 |
str(sourceIP) + ' ' + |
| 153 |
str(destinationIP) + ' ' + |
| 154 |
hex(timeStamp)[2:] + ' ' + |
| 155 |
str(roundNumber) + ' ' + |
| 156 |
hex(checksum)[2:] + ' ' + |
| 157 |
str(totalHops) + ' ' + |
| 158 |
hex(statusFlags)[2:] + ' ' + |
| 159 |
hex(pathHash)[2:] + ' ' + |
| 160 |
hex(trafficClass)[2:] + ' ' + |
| 161 |
str(packetSize) + '\n') |
| 162 |
sys.stdout.write('\t ' + |
| 163 |
str(hopNumber) + ' ' + |
| 164 |
hex(status & 0xff)[2:] + ' ' + |
| 165 |
str(rtt) + ' ' + |
| 166 |
str(hopIP) + '\n') |
| 167 |
|
| 168 |
# ------ NoSQL: MongoDB -------------------------------------------------- |
| 169 |
elif configuration.getBackend() == 'MongoDB': |
| 170 |
query = { } |
| 171 |
if fromTimeStamp != None: |
| 172 |
ft = { 'timestamp': { '$gte': str(fromTimeStamp) } } |
| 173 |
if toTimeStamp != None: |
| 174 |
tt = { 'timestamp': { '$lt': str(toTimeStamp) } } |
| 175 |
query = { '$and': [ ft, tt ] } |
| 176 |
else: |
| 177 |
query = ft |
| 178 |
rows = configuration.queryMongoDB('traceroute', query) |
| 179 |
|
| 180 |
for row in rows: |
| 181 |
timeStamp = int(row['timestamp']) |
| 182 |
sourceIP = ipaddress.ip_address(bytes(row['source'])) |
| 183 |
destinationIP = ipaddress.ip_address(bytes(row['destination'])) |
| 184 |
roundNumber = int(row['round']) |
| 185 |
checksum = int(row['checksum']) |
| 186 |
packetSize = int(row['pktsize']) |
| 187 |
trafficClass = int(row['tc']) |
| 188 |
totalHops = int(row['totalHops']) |
| 189 |
statusFlags = int(row['statusFlags']) |
| 190 |
pathHash = int(row['pathHash']) |
| 191 |
if pathHash < 0: |
| 192 |
pathHash = 0x10000000000000000 - abs(pathHash) |
| 193 |
sys.stdout.write('#T ' + |
| 194 |
str(sourceIP) + ' ' + |
| 195 |
str(destinationIP) + ' ' + |
| 196 |
hex(timeStamp)[2:] + ' ' + |
| 197 |
str(roundNumber) + ' ' + |
| 198 |
hex(checksum)[2:] + ' ' + |
| 199 |
str(totalHops) + ' ' + |
| 200 |
hex(statusFlags)[2:] + ' ' + |
| 201 |
hex(pathHash)[2:] + ' ' + |
| 202 |
hex(trafficClass)[2:] + ' ' + |
| 203 |
str(packetSize) + '\n') |
| 204 |
|
| 205 |
hopNumber = 0 |
| 206 |
for hop in row['hops']: |
| 207 |
hopNumber = hopNumber + 1 |
| 208 |
hopIP = ipaddress.ip_address(bytes(hop['hop'])) |
| 209 |
status = int(hop['status']) |
| 210 |
rtt = int(hop['rtt']) |
| 211 |
sys.stdout.write('\t ' + |
| 212 |
str(hopNumber) + ' ' + |
| 213 |
hex(status)[2:] + ' ' + |
| 214 |
str(rtt) + ' ' + |
| 215 |
str(hopIP) + '\n') |
| 216 |
|
| 217 |
|
| 218 |
# ====== Error ============================================================= |
| 219 |
else: |
| 220 |
sys.stderr.write('Specify query type!\n') |
| 221 |
sys.exit(1) |