node_exporter指标数据python处理20240624[网卡接收、发送的总字节数]

node_exporter指标数据python处理20240624

 

获取node_exporter的metrics的值,并存入字典中。___20240625

import requests

import re



req = requests.get('http://121.37.14.19:9100/metrics')

# print(req.request.headers)

# print(req.text)



data=req.text

# print(data)

data2=data.split('\n')

# print(type(data2))

c=dict()

for i in data2:

 result1 = re.match('[^#]', i, 1)

 if result1:

 # print(i)

 data3=i.rsplit(' ',1)

 # print(data3)

 c.update({data3[0]:data3[1]})

print(c)

for k,v in c.items():

 print("Key: {} Value: {}".format(k,v))

# print(type(c))

# c的结果为: {'go_gc_duration_seconds{quantile="0"}': '3.3516e-05', 'go_gc_duration_seconds{quantile="0.25"}': '4.0037e-05', 'go_gc_duration_seconds{quantile="0.5"}': '4.6082e-05', 'go_gc_duration_seconds{quantile="0.75"}': '5.69e-05', 'go_gc_duration_seconds{quantile="1"}': '0.001609138', 'go_gc_duration_seconds_sum': '0.824608361', 'go_gc_duration_seconds_count': '15327', 'go_goroutines': '8', 'go_info{version="go1.22.2"}': '1', 'go_memstats_alloc_bytes': '1.998208e+06', 'go_memstats_alloc_bytes_total': '2.813685308e+10', 'go_memstats_buck_hash_sys_bytes': '1.958114e+06', 'go_memstats_frees_total': '3.68503637e+08', 'go_memstats_gc_sys_bytes': '3.256144e+06', 'go_memstats_heap_alloc_bytes': '1.998208e+06', 'go_memstats_heap_idle_bytes': '4.284416e+06', 'go_memstats_heap_inuse_bytes': '3.596288e+06', 'go_memstats_heap_objects': '11998', 'go_memstats_heap_released_bytes': '3.129344e+06', 'go_memstats_heap_sys_bytes': '7.880704e+06', 'go_memstats_last_gc_time_seconds': '1.7193003528705318e+09', 'go_memstats_lookups_total': '0', 'go_memstats_mallocs_total': '3.68515635e+08', 'go_memstats_mcache_inuse_bytes': '1200', 'go_memstats_mcache_sys_bytes': '15600', 'go_memstats_mspan_inuse_bytes': '65760', 'go_memstats_mspan_sys_bytes': '97920', 'go_memstats_next_gc_bytes': '4.261128e+06', 'go_memstats_other_sys_bytes': '805222', 'go_memstats_stack_inuse_bytes': '491520', 'go_memstats_stack_sys_bytes': '491520', 'go_memstats_sys_bytes': '1.4505224e+07', 'go_threads': '5', 'node_arp_entries{device="eth0"}': '4', 'node_boot_time_seconds': '1.716533868e+09', 'node_context_switches_total': '1.06522272e+09', 'node_cooling_device_cur_state{name="0",type="Processor"}': '0', 'node_cooling_device_cur_state{name="1",type="Processor"}': '0', 'node_cooling_device_max_state{name="0",type="Processor"}': '0', 'node_cooling_device_max_state{name="1",type="Processor"}': '0', 'node_cpu_guest_seconds_total{cpu="0",mode="nice"}': '0', 'node_cpu_guest_seconds_total{cpu="0",mode="user"}': '0', 'node_cpu_guest_seconds_total{cpu="1",mode="nice"}': '0', 'node_cpu_guest_seconds_total{cpu="1",mode="user"}': '0', 'node_cpu_seconds_total{cpu="0",mode="idle"}': '2.70341855e+06', 'node_cpu_seconds_total{cpu="0",mode="iowait"}': '833.99', 'node_cpu_seconds_total{cpu="0",mode="irq"}': '0', 'node_cpu_seconds_total{cpu="0",mode="nice"}': '96.22', 'node_cpu_seconds_total{cpu="0",mode="softirq"}': '64.66', 'node_cpu_seconds_total{cpu="0",mode="steal"}': '0', 'node_cpu_seconds_total{cpu="0",mode="system"}': '3494.7', 'node_cpu_seconds_total{cpu="0",mode="user"}': '51947.86', 'node_cpu_seconds_total{cpu="1",mode="idle"}': '2.70897602e+06', 'node_cpu_seconds_total{cpu="1",mode="iowait"}': '756.26', 'node_cpu_seconds_total{cpu="1",mode="irq"}': '0', 'node_cpu_seconds_total{cpu="1",mode="nice"}': '101.3', 'node_cpu_seconds_total{cpu="1",mode="softirq"}': '190.07', 'node_cpu_seconds_total{cpu="1",mode="steal"}': '0', 'node_cpu_seconds_total{cpu="1",mode="system"}': '3512.83', 'node_cpu_seconds_total{cpu="1",mode="user"}': '44790.68', 'node_disk_discard_time_seconds_total{device="vda"}': '0', 'node_disk_discarded_sectors_total{device="vda"}': '0', 'node_disk_discards_completed_total{device="vda"}': '0', 'node_disk_discards_merged_total{device="vda"}': '0', 'node_disk_flush_requests_time_seconds_total{device="vda"}': '96.842', 'node_disk_flush_requests_total{device="vda"}': '1.010585e+06', 'node_disk_info{device="vda",major="252",minor="0",model="",path="pci-0000:02:01.0",revision="",serial="",wwn=""}': '1', 'node_disk_io_now{device="vda"}': '0', 'node_disk_io_time_seconds_total{device="vda"}': '5294.752', 'node_disk_io_time_weighted_seconds_total{device="vda"}': '6640.962', 'node_disk_read_bytes_total{device="vda"}': '6.47553536e+09', 'node_disk_read_time_seconds_total{device="vda"}': '408.755', 'node_disk_reads_completed_total{device="vda"}': '220512', 'node_disk_reads_merged_total{device="vda"}': '48279', 'node_disk_write_time_seconds_total{device="vda"}': '6135.363', 'node_disk_writes_completed_total{device="vda"}': '5.565994e+06', 'node_disk_writes_merged_total{device="vda"}': '2.635608e+06', 'node_disk_written_bytes_total{device="vda"}': '4.7383658496e+10', 'node_dmi_info{bios_date="04/01/2014",bios_release="0.0",bios_vendor="SeaBIOS",bios_version="rel-1.12.1-0-ga5cab58-20230409_150425-szxrtosci10000",chassis_asset_tag="HUAWEICLOUD",chassis_serial="",chassis_vendor="",chassis_version="pc-i440fx-4.1",product_family="Virtual Machine",product_name="OpenStack Nova",product_serial="58c5d596-10c5-4c03-b653-ceb706d1c81f",product_sku="",product_uuid="58c5d596-10c5-4c03-b653-ceb706d1c81f",product_version="13.2.1-20240313170415_1bdeaca",system_vendor="OpenStack Foundation"}': '1', 'node_entropy_available_bits': '256', 'node_entropy_pool_size_bits': '256', 'node_exporter_build_info{branch="HEAD",goarch="amd64",goos="linux",goversion="go1.22.2",revision="cadb1d1190ad95c66b951758f01ff4c94e55e6ce",tags="unknown",version="1.8.0"}': '1', 'node_filefd_allocated': '2080', 'node_filefd_maximum': '9.223372036854776e+18', 'node_filesystem_avail_bytes{device="/dev/vda1",device_error="",fstype="ext4",mountpoint="/"}': '2.4133373952e+10', 'node_filesystem_avail_bytes{device="tmpfs",device_error="",fstype="tmpfs",mountpoint="/run"}': '1.84541184e+08', 'node_filesystem_avail_bytes{device="tmpfs",device_error="",fstype="tmpfs",mountpoint="/run/lock"}': '5.24288e+06', 'node_filesystem_avail_bytes{device="tmpfs",device_error="",fstype="tmpfs",mountpoint="/run/snapd/ns"}': '1.84541184e+08', 'node_filesystem_avail_bytes{device="tmpfs",device_error="",fstype="tmpfs",mountpoint="/run/user/0"}': '1.85708544e+08', 'node_filesystem_device_error{device="/dev/vda1",device_error="",fstype="ext4",mountpoint="/"}': '0', 'node_filesystem_device_error{device="tmpfs",device_error="",fstype="tmpfs",mountpoint="/run"}': '0', 'node_filesystem_device_error{device="tmpfs",device_error="",fstype="tmpfs",mountpoint="/run/lock"}': '0', 'node_filesystem_device_error{device="tmpfs",device_error="",fstype="tmpfs",mountpoint="/run/snapd/ns"}': '0', 'node_filesystem_device_error{device="tmpfs",device_error="",fstype="tmpfs",mountpoint="/run/user/0"}': '0', 'node_filesystem_files{device="/dev/vda1",device_error="",fstype="ext4",mountpoint="/"}': '2.62144e+06', 'node_filesystem_files{device="tmpfs",device_error="",fstype="tmpfs",mountpoint="/run"}': '226732', 'node_filesystem_files{device="tmpfs",device_error="",fstype="tmpfs",mountpoint="/run/lock"}': '226732', 'node_filesystem_files{device="tmpfs",device_error="",fstype="tmpfs",mountpoint="/run/snapd/ns"}': '226732', 'node_filesystem_files{device="tmpfs",device_error="",fstype="tmpfs",mountpoint="/run/user/0"}': '45346', 'node_filesystem_files_free{device="/dev/vda1",device_error="",fstype="ext4",mountpoint="/"}': '2.358274e+06', 'node_filesystem_files_free{device="tmpfs",device_error="",fstype="tmpfs",mountpoint="/run"}': '225966', 'node_filesystem_files_free{device="tmpfs",device_error="",fstype="tmpfs",mountpoint="/run/lock"}': '226729', 'node_filesystem_files_free{device="tmpfs",device_error="",fstype="tmpfs",mountpoint="/run/snapd/ns"}': '225966', 'node_filesystem_files_free{device="tmpfs",device_error="",fstype="tmpfs",mountpoint="/run/user/0"}': '45301', 'node_filesystem_free_bytes{device="/dev/vda1",device_error="",fstype="ext4",mountpoint="/"}': '2.629752832e+10', 'node_filesystem_free_bytes{device="tmpfs",device_error="",fstype="tmpfs",mountpoint="/run"}': '1.84541184e+08', 'node_filesystem_free_bytes{device="tmpfs",device_error="",fstype="tmpfs",mountpoint="/run/lock"}': '5.24288e+06', 'node_filesystem_free_bytes{device="tmpfs",device_error="",fstype="tmpfs",mountpoint="/run/snapd/ns"}': '1.84541184e+08', 'node_filesystem_free_bytes{device="tmpfs",device_error="",fstype="tmpfs",mountpoint="/run/user/0"}': '1.85708544e+08', 'node_filesystem_readonly{device="/dev/vda1",device_error="",fstype="ext4",mountpoint="/"}': '0', 'node_filesystem_readonly{device="tmpfs",device_error="",fstype="tmpfs",mountpoint="/run"}': '0', 'node_filesystem_readonly{device="tmpfs",device_error="",fstype="tmpfs",mountpoint="/run/lock"}': '0', 'node_filesystem_readonly{device="tmpfs",device_error="",fstype="tmpfs",mountpoint="/run/snapd/ns"}': '0', 'node_filesystem_readonly{device="tmpfs",device_error="",fstype="tmpfs",mountpoint="/run/user/0"}': '0', 'node_filesystem_size_bytes{device="/dev/vda1",device_error="",fstype="ext4",mountpoint="/"}': '4.1954803712e+10', 'node_filesystem_size_bytes{device="tmpfs",device_error="",fstype="tmpfs",mountpoint="/run"}': '1.85741312e+08', 'node_filesystem_size_bytes{device="tmpfs",device_error="",fstype="tmpfs",mountpoint="/run/lock"}': '5.24288e+06', 'node_filesystem_size_bytes{device="tmpfs",device_error="",fstype="tmpfs",mountpoint="/run/snapd/ns"}': '1.85741312e+08', 'node_filesystem_size_bytes{device="tmpfs",device_error="",fstype="tmpfs",mountpoint="/run/user/0"}': '1.85737216e+08', 'node_forks_total': '376883', 'node_intr_total': '6.08008224e+08', 'node_load1': '0.03', 'node_load15': '0.08', 'node_load5': '0.08', 'node_memory_Active_anon_bytes': '1.114112e+06', 'node_memory_Active_bytes': '4.82840576e+08', 'node_memory_Active_file_bytes': '4.81726464e+08', 'node_memory_AnonHugePages_bytes': '0', 'node_memory_AnonPages_bytes': '5.23472896e+08', 'node_memory_Bounce_bytes': '0', 'node_memory_Buffers_bytes': '1.94535424e+08', 'node_memory_Cached_bytes': '7.104512e+08', 'node_memory_CommitLimit_bytes': '9.28694272e+08', 'node_memory_Committed_AS_bytes': '2.342981632e+09', 'node_memory_DirectMap1G_bytes': '0', 'node_memory_DirectMap2M_bytes': '1.90840832e+09', 'node_memory_DirectMap4k_bytes': '2.3855104e+08', 'node_memory_Dirty_bytes': '57344', 'node_memory_FileHugePages_bytes': '0', 'node_memory_FilePmdMapped_bytes': '0', 'node_memory_HardwareCorrupted_bytes': '0', 'node_memory_HugePages_Free': '0', 'node_memory_HugePages_Rsvd': '0', 'node_memory_HugePages_Surp': '0', 'node_memory_HugePages_Total': '0', 'node_memory_Hugepagesize_bytes': '2.097152e+06', 'node_memory_Hugetlb_bytes': '0', 'node_memory_Inactive_anon_bytes': '5.04725504e+08', 'node_memory_Inactive_bytes': '9.15902464e+08', 'node_memory_Inactive_file_bytes': '4.1117696e+08', 'node_memory_KReclaimable_bytes': '1.28659456e+08', 'node_memory_KernelStack_bytes': '4.42368e+06', 'node_memory_Mapped_bytes': '1.93019904e+08', 'node_memory_MemAvailable_bytes': '9.9270656e+08', 'node_memory_MemFree_bytes': '1.62390016e+08', 'node_memory_MemTotal_bytes': '1.85739264e+09', 'node_memory_Mlocked_bytes': '2.8143616e+07', 'node_memory_NFS_Unstable_bytes': '0', 'node_memory_PageTables_bytes': '7.122944e+06', 'node_memory_Percpu_bytes': '1.400832e+06', 'node_memory_SReclaimable_bytes': '1.28659456e+08', 'node_memory_SUnreclaim_bytes': '5.6700928e+07', 'node_memory_ShmemHugePages_bytes': '0', 'node_memory_ShmemPmdMapped_bytes': '0', 'node_memory_Shmem_bytes': '2.801664e+06', 'node_memory_Slab_bytes': '1.85360384e+08', 'node_memory_SwapCached_bytes': '0', 'node_memory_SwapFree_bytes': '0', 'node_memory_SwapTotal_bytes': '0', 'node_memory_Unevictable_bytes': '2.971648e+07', 'node_memory_VmallocChunk_bytes': '0', 'node_memory_VmallocTotal_bytes': '3.5184372087808e+13', 'node_memory_VmallocUsed_bytes': '1.8321408e+07', 'node_memory_WritebackTmp_bytes': '0', 'node_memory_Writeback_bytes': '0', 'node_netstat_Icmp6_InErrors': '0', 'node_netstat_Icmp6_InMsgs': '5', 'node_netstat_Icmp6_OutMsgs': '841', 'node_netstat_Icmp_InErrors': '228', 'node_netstat_Icmp_InMsgs': '126153', 'node_netstat_Icmp_OutMsgs': '201224', 'node_netstat_Ip6_InOctets': '6722', 'node_netstat_Ip6_OutOctets': '54910', 'node_netstat_IpExt_InOctets': '3.187090361e+09', 'node_netstat_IpExt_OutOctets': '1.821539083e+09', 'node_netstat_Ip_Forwarding': '1', 'node_netstat_TcpExt_ListenDrops': '0', 'node_netstat_TcpExt_ListenOverflows': '0', 'node_netstat_TcpExt_SyncookiesFailed': '0', 'node_netstat_TcpExt_SyncookiesRecv': '0', 'node_netstat_TcpExt_SyncookiesSent': '0', 'node_netstat_TcpExt_TCPOFOQueue': '243366', 'node_netstat_TcpExt_TCPSynRetrans': '7371', 'node_netstat_TcpExt_TCPTimeouts': '15320', 'node_netstat_Tcp_ActiveOpens': '115876', 'node_netstat_Tcp_CurrEstab': '21', 'node_netstat_Tcp_InErrs': '278', 'node_netstat_Tcp_InSegs': '7.528337e+06', 'node_netstat_Tcp_OutRsts': '1.861089e+06', 'node_netstat_Tcp_OutSegs': '6.429648e+06', 'node_netstat_Tcp_PassiveOpens': '130155', 'node_netstat_Tcp_RetransSegs': '48744', 'node_netstat_Udp6_InDatagrams': '30', 'node_netstat_Udp6_InErrors': '0', 'node_netstat_Udp6_NoPorts': '3', 'node_netstat_Udp6_OutDatagrams': '33', 'node_netstat_Udp6_RcvbufErrors': '0', 'node_netstat_Udp6_SndbufErrors': '0', 'node_netstat_UdpLite6_InErrors': '0', 'node_netstat_UdpLite_InErrors': '0', 'node_netstat_Udp_InDatagrams': '351872', 'node_netstat_Udp_InErrors': '134', 'node_netstat_Udp_NoPorts': '33496', 'node_netstat_Udp_OutDatagrams': '351569', 'node_netstat_Udp_RcvbufErrors': '0', 'node_netstat_Udp_SndbufErrors': '0', 'node_network_address_assign_type{device="docker0"}': '3', 'node_network_address_assign_type{device="eth0"}': '0', 'node_network_address_assign_type{device="lo"}': '0', 'node_network_carrier{device="docker0"}': '0', 'node_network_carrier{device="eth0"}': '1', 'node_network_carrier{device="lo"}': '1', 'node_network_carrier_changes_total{device="docker0"}': '11', 'node_network_carrier_changes_total{device="eth0"}': '2', 'node_network_carrier_changes_total{device="lo"}': '0', 'node_network_carrier_down_changes_total{device="docker0"}': '6', 'node_network_carrier_down_changes_total{device="eth0"}': '1', 'node_network_carrier_down_changes_total{device="lo"}': '0', 'node_network_carrier_up_changes_total{device="docker0"}': '5', 'node_network_carrier_up_changes_total{device="eth0"}': '1', 'node_network_carrier_up_changes_total{device="lo"}': '0', 'node_network_device_id{device="docker0"}': '0', 'node_network_device_id{device="eth0"}': '0', 'node_network_device_id{device="lo"}': '0', 'node_network_dormant{device="docker0"}': '0', 'node_network_dormant{device="eth0"}': '0', 'node_network_dormant{device="lo"}': '0', 'node_network_flags{device="docker0"}': '4099', 'node_network_flags{device="eth0"}': '4099', 'node_network_flags{device="lo"}': '9', 'node_network_iface_id{device="docker0"}': '3', 'node_network_iface_id{device="eth0"}': '2', 'node_network_iface_id{device="lo"}': '1', 'node_network_iface_link{device="docker0"}': '3', 'node_network_iface_link{device="eth0"}': '2', 'node_network_iface_link{device="lo"}': '1', 'node_network_iface_link_mode{device="docker0"}': '0', 'node_network_iface_link_mode{device="eth0"}': '0', 'node_network_iface_link_mode{device="lo"}': '0', 'node_network_info{address="00:00:00:00:00:00",adminstate="up",broadcast="00:00:00:00:00:00",device="lo",duplex="",ifalias="",operstate="unknown"}': '1', 'node_network_info{address="02:42:bc:be:b8:f2",adminstate="up",broadcast="ff:ff:ff:ff:ff:ff",device="docker0",duplex="unknown",ifalias="",operstate="down"}': '1', 'node_network_info{address="fa:16:3e:d1:32:19",adminstate="up",broadcast="ff:ff:ff:ff:ff:ff",device="eth0",duplex="unknown",ifalias="",operstate="up"}': '1', 'node_network_mtu_bytes{device="docker0"}': '1500', 'node_network_mtu_bytes{device="eth0"}': '1500', 'node_network_mtu_bytes{device="lo"}': '65536', 'node_network_name_assign_type{device="docker0"}': '3', 'node_network_name_assign_type{device="lo"}': '2', 'node_network_net_dev_group{device="docker0"}': '0', 'node_network_net_dev_group{device="eth0"}': '0', 'node_network_net_dev_group{device="lo"}': '0', 'node_network_protocol_type{device="docker0"}': '1', 'node_network_protocol_type{device="eth0"}': '1', 'node_network_protocol_type{device="lo"}': '772', 'node_network_receive_bytes_total{device="docker0"}': '22404', 'node_network_receive_bytes_total{device="eth0"}': '2.722989458e+09', 'node_network_receive_bytes_total{device="lo"}': '6.03678684e+08', 'node_network_receive_compressed_total{device="docker0"}': '0', 'node_network_receive_compressed_total{device="eth0"}': '0', 'node_network_receive_compressed_total{device="lo"}': '0', 'node_network_receive_drop_total{device="docker0"}': '0', 'node_network_receive_drop_total{device="eth0"}': '0', 'node_network_receive_drop_total{device="lo"}': '0', 'node_network_receive_errs_total{device="docker0"}': '0', 'node_network_receive_errs_total{device="eth0"}': '0', 'node_network_receive_errs_total{device="lo"}': '0', 'node_network_receive_fifo_total{device="docker0"}': '0', 'node_network_receive_fifo_total{device="eth0"}': '0', 'node_network_receive_fifo_total{device="lo"}': '0', 'node_network_receive_frame_total{device="docker0"}': '0', 'node_network_receive_frame_total{device="eth0"}': '0', 'node_network_receive_frame_total{device="lo"}': '0', 'node_network_receive_multicast_total{device="docker0"}': '0', 'node_network_receive_multicast_total{device="eth0"}': '0', 'node_network_receive_multicast_total{device="lo"}': '0', 'node_network_receive_nohandler_total{device="docker0"}': '0', 'node_network_receive_nohandler_total{device="eth0"}': '0', 'node_network_receive_nohandler_total{device="lo"}': '0', 'node_network_receive_packets_total{device="docker0"}': '86', 'node_network_receive_packets_total{device="eth0"}': '8.007238e+06', 'node_network_receive_packets_total{device="lo"}': '607770', 'node_network_speed_bytes{device="docker0"}': '-125000', 'node_network_speed_bytes{device="eth0"}': '-125000', 'node_network_transmit_bytes_total{device="docker0"}': '12895', 'node_network_transmit_bytes_total{device="eth0"}': '1.306469274e+09', 'node_network_transmit_bytes_total{device="lo"}': '6.03678684e+08', 'node_network_transmit_carrier_total{device="docker0"}': '0', 'node_network_transmit_carrier_total{device="eth0"}': '0', 'node_network_transmit_carrier_total{device="lo"}': '0', 'node_network_transmit_colls_total{device="docker0"}': '0', 'node_network_transmit_colls_total{device="eth0"}': '0', 'node_network_transmit_colls_total{device="lo"}': '0', 'node_network_transmit_compressed_total{device="docker0"}': '0', 'node_network_transmit_compressed_total{device="eth0"}': '0', 'node_network_transmit_compressed_total{device="lo"}': '0', 'node_network_transmit_drop_total{device="docker0"}': '0', 'node_network_transmit_drop_total{device="eth0"}': '0', 'node_network_transmit_drop_total{device="lo"}': '0', 'node_network_transmit_errs_total{device="docker0"}': '0', 'node_network_transmit_errs_total{device="eth0"}': '0', 'node_network_transmit_errs_total{device="lo"}': '0', 'node_network_transmit_fifo_total{device="docker0"}': '0', 'node_network_transmit_fifo_total{device="eth0"}': '0', 'node_network_transmit_fifo_total{device="lo"}': '0', 'node_network_transmit_packets_total{device="docker0"}': '124', 'node_network_transmit_packets_total{device="eth0"}': '6.314715e+06', 'node_network_transmit_packets_total{device="lo"}': '607770', 'node_network_transmit_queue_length{device="docker0"}': '0', 'node_network_transmit_queue_length{device="eth0"}': '1000', 'node_network_transmit_queue_length{device="lo"}': '1000', 'node_network_up{device="docker0"}': '0', 'node_network_up{device="eth0"}': '1', 'node_network_up{device="lo"}': '0', 'node_nf_conntrack_entries': '25', 'node_nf_conntrack_entries_limit': '65536', 'node_os_info{build_id="",id="ubuntu",id_like="debian",image_id="",image_version="",name="Ubuntu",pretty_name="Ubuntu 22.04.2 LTS",variant="",variant_id="",version="22.04.2 LTS (Jammy Jellyfish)",version_codename="jammy",version_id="22.04"}': '1', 'node_os_version{id="ubuntu",id_like="debian",name="Ubuntu"}': '22.04', 'node_pressure_cpu_waiting_seconds_total': '4160.034296', 'node_pressure_io_stalled_seconds_total': '947.775969', 'node_pressure_io_waiting_seconds_total': '981.2887049999999', 'node_pressure_memory_stalled_seconds_total': '21.097699000000002', 'node_pressure_memory_waiting_seconds_total': '22.726439', 'node_procs_blocked': '0', 'node_procs_running': '1', 'node_schedstat_running_seconds_total{cpu="0"}': '59281.422425403', 'node_schedstat_running_seconds_total{cpu="1"}': '52350.92440526', 'node_schedstat_timeslices_total{cpu="0"}': '2.73510787e+08', 'node_schedstat_timeslices_total{cpu="1"}': '2.82501961e+08', 'node_schedstat_waiting_seconds_total{cpu="0"}': '758.113741211', 'node_schedstat_waiting_seconds_total{cpu="1"}': '908.907532277', 'node_scrape_collector_duration_seconds{collector="arp"}': '0.000169265', 'node_scrape_collector_duration_seconds{collector="bcache"}': '6.443e-06', 'node_scrape_collector_duration_seconds{collector="bonding"}': '9.562e-06', 'node_scrape_collector_duration_seconds{collector="btrfs"}': '0.000110491', 'node_scrape_collector_duration_seconds{collector="conntrack"}': '6.3789e-05', 'node_scrape_collector_duration_seconds{collector="cpu"}': '0.000183822', 'node_scrape_collector_duration_seconds{collector="cpufreq"}': '3.9332e-05', 'node_scrape_collector_duration_seconds{collector="diskstats"}': '0.000199368', 'node_scrape_collector_duration_seconds{collector="dmi"}': '1.5986e-05', 'node_scrape_collector_duration_seconds{collector="edac"}': '3.0575e-05', 'node_scrape_collector_duration_seconds{collector="entropy"}': '6.2312e-05', 'node_scrape_collector_duration_seconds{collector="fibrechannel"}': '7.499e-06', 'node_scrape_collector_duration_seconds{collector="filefd"}': '1.9382e-05', 'node_scrape_collector_duration_seconds{collector="filesystem"}': '0.003175979', 'node_scrape_collector_duration_seconds{collector="hwmon"}': '1.642e-05', 'node_scrape_collector_duration_seconds{collector="infiniband"}': '6.345e-06', 'node_scrape_collector_duration_seconds{collector="ipvs"}': '1.3919e-05', 'node_scrape_collector_duration_seconds{collector="loadavg"}': '2.4155e-05', 'node_scrape_collector_duration_seconds{collector="mdadm"}': '2.9149e-05', 'node_scrape_collector_duration_seconds{collector="meminfo"}': '0.000117619', 'node_scrape_collector_duration_seconds{collector="netclass"}': '0.001149648', 'node_scrape_collector_duration_seconds{collector="netdev"}': '0.000225519', 'node_scrape_collector_duration_seconds{collector="netstat"}': '0.000777129', 'node_scrape_collector_duration_seconds{collector="nfs"}': '1.2321e-05', 'node_scrape_collector_duration_seconds{collector="nfsd"}': '1.2826e-05', 'node_scrape_collector_duration_seconds{collector="nvme"}': '1.6107e-05', 'node_scrape_collector_duration_seconds{collector="os"}': '3.5125e-05', 'node_scrape_collector_duration_seconds{collector="powersupplyclass"}': '2.7537e-05', 'node_scrape_collector_duration_seconds{collector="pressure"}': '7.8069e-05', 'node_scrape_collector_duration_seconds{collector="rapl"}': '2.1602e-05', 'node_scrape_collector_duration_seconds{collector="schedstat"}': '6.9193e-05', 'node_scrape_collector_duration_seconds{collector="selinux"}': '1.92e-06', 'node_scrape_collector_duration_seconds{collector="sockstat"}': '9.2339e-05', 'node_scrape_collector_duration_seconds{collector="softnet"}': '5.8612e-05', 'node_scrape_collector_duration_seconds{collector="stat"}': '9.3515e-05', 'node_scrape_collector_duration_seconds{collector="tapestats"}': '7.876e-06', 'node_scrape_collector_duration_seconds{collector="textfile"}': '2.1314e-05', 'node_scrape_collector_duration_seconds{collector="thermal_zone"}': '9.0001e-05', 'node_scrape_collector_duration_seconds{collector="time"}': '8.5331e-05', 'node_scrape_collector_duration_seconds{collector="timex"}': '2.3579e-05', 'node_scrape_collector_duration_seconds{collector="udp_queues"}': '7.5158e-05', 'node_scrape_collector_duration_seconds{collector="uname"}': '1.0569e-05', 'node_scrape_collector_duration_seconds{collector="vmstat"}': '0.000111668', 'node_scrape_collector_duration_seconds{collector="watchdog"}': '1.4493e-05', 'node_scrape_collector_duration_seconds{collector="xfs"}': '7.137e-06', 'node_scrape_collector_duration_seconds{collector="zfs"}': '5.9313e-05', 'node_scrape_collector_success{collector="arp"}': '1', 'node_scrape_collector_success{collector="bcache"}': '1', 'node_scrape_collector_success{collector="bonding"}': '0', 'node_scrape_collector_success{collector="btrfs"}': '1', 'node_scrape_collector_success{collector="conntrack"}': '0', 'node_scrape_collector_success{collector="cpu"}': '1', 'node_scrape_collector_success{collector="cpufreq"}': '1', 'node_scrape_collector_success{collector="diskstats"}': '1', 'node_scrape_collector_success{collector="dmi"}': '1', 'node_scrape_collector_success{collector="edac"}': '1', 'node_scrape_collector_success{collector="entropy"}': '1', 'node_scrape_collector_success{collector="fibrechannel"}': '0', 'node_scrape_collector_success{collector="filefd"}': '1', 'node_scrape_collector_success{collector="filesystem"}': '1', 'node_scrape_collector_success{collector="hwmon"}': '1', 'node_scrape_collector_success{collector="infiniband"}': '0', 'node_scrape_collector_success{collector="ipvs"}': '0', 'node_scrape_collector_success{collector="loadavg"}': '1', 'node_scrape_collector_success{collector="mdadm"}': '1', 'node_scrape_collector_success{collector="meminfo"}': '1', 'node_scrape_collector_success{collector="netclass"}': '1', 'node_scrape_collector_success{collector="netdev"}': '1', 'node_scrape_collector_success{collector="netstat"}': '1', 'node_scrape_collector_success{collector="nfs"}': '0', 'node_scrape_collector_success{collector="nfsd"}': '0', 'node_scrape_collector_success{collector="nvme"}': '0', 'node_scrape_collector_success{collector="os"}': '1', 'node_scrape_collector_success{collector="powersupplyclass"}': '1', 'node_scrape_collector_success{collector="pressure"}': '1', 'node_scrape_collector_success{collector="rapl"}': '1', 'node_scrape_collector_success{collector="schedstat"}': '1', 'node_scrape_collector_success{collector="selinux"}': '1', 'node_scrape_collector_success{collector="sockstat"}': '1', 'node_scrape_collector_success{collector="softnet"}': '1', 'node_scrape_collector_success{collector="stat"}': '1', 'node_scrape_collector_success{collector="tapestats"}': '0', 'node_scrape_collector_success{collector="textfile"}': '1', 'node_scrape_collector_success{collector="thermal_zone"}': '1', 'node_scrape_collector_success{collector="time"}': '1', 'node_scrape_collector_success{collector="timex"}': '1', 'node_scrape_collector_success{collector="udp_queues"}': '1', 'node_scrape_collector_success{collector="uname"}': '1', 'node_scrape_collector_success{collector="vmstat"}': '1', 'node_scrape_collector_success{collector="watchdog"}': '1', 'node_scrape_collector_success{collector="xfs"}': '1', 'node_scrape_collector_success{collector="zfs"}': '0', 'node_selinux_enabled': '0', 'node_sockstat_FRAG6_inuse': '0', 'node_sockstat_FRAG6_memory': '0', 'node_sockstat_FRAG_inuse': '0', 'node_sockstat_FRAG_memory': '0', 'node_sockstat_RAW6_inuse': '0', 'node_sockstat_RAW_inuse': '0', 'node_sockstat_TCP6_inuse': '12', 'node_sockstat_TCP_alloc': '42', 'node_sockstat_TCP_inuse': '30', 'node_sockstat_TCP_mem': '4', 'node_sockstat_TCP_mem_bytes': '16384', 'node_sockstat_TCP_orphan': '0', 'node_sockstat_TCP_tw': '1', 'node_sockstat_UDP6_inuse': '3', 'node_sockstat_UDPLITE6_inuse': '0', 'node_sockstat_UDPLITE_inuse': '0', 'node_sockstat_UDP_inuse': '6', 'node_sockstat_UDP_mem': '4', 'node_sockstat_UDP_mem_bytes': '16384', 'node_sockstat_sockets_used': '277', 'node_softnet_backlog_len{cpu="0"}': '0', 'node_softnet_backlog_len{cpu="1"}': '0', 'node_softnet_cpu_collision_total{cpu="0"}': '0', 'node_softnet_cpu_collision_total{cpu="1"}': '0', 'node_softnet_dropped_total{cpu="0"}': '0', 'node_softnet_dropped_total{cpu="1"}': '0', 'node_softnet_flow_limit_count_total{cpu="0"}': '0', 'node_softnet_flow_limit_count_total{cpu="1"}': '0', 'node_softnet_processed_total{cpu="0"}': '4.024596e+06', 'node_softnet_processed_total{cpu="1"}': '4.023112e+06', 'node_softnet_received_rps_total{cpu="0"}': '3.430521e+06', 'node_softnet_received_rps_total{cpu="1"}': '11', 'node_softnet_times_squeezed_total{cpu="0"}': '14', 'node_softnet_times_squeezed_total{cpu="1"}': '22', 'node_textfile_scrape_error': '0', 'node_time_clocksource_available_info{clocksource="acpi_pm",device="0"}': '1', 'node_time_clocksource_available_info{clocksource="kvm-clock",device="0"}': '1', 'node_time_clocksource_available_info{clocksource="tsc",device="0"}': '1', 'node_time_clocksource_current_info{clocksource="tsc",device="0"}': '1', 'node_time_seconds': '1.7193003623997447e+09', 'node_time_zone_offset_seconds{time_zone="CST"}': '28800', 'node_timex_estimated_error_seconds': '5.5e-05', 'node_timex_frequency_adjustment_ratio': '1.0000233918151855', 'node_timex_loop_time_constant': '2', 'node_timex_maxerror_seconds': '0.454405', 'node_timex_offset_seconds': '0', 'node_timex_pps_calibration_total': '0', 'node_timex_pps_error_total': '0', 'node_timex_pps_frequency_hertz': '0', 'node_timex_pps_jitter_seconds': '0', 'node_timex_pps_jitter_total': '0', 'node_timex_pps_shift_seconds': '0', 'node_timex_pps_stability_exceeded_total': '0', 'node_timex_pps_stability_hertz': '0', 'node_timex_status': '0', 'node_timex_sync_status': '1', 'node_timex_tai_offset_seconds': '0', 'node_timex_tick_seconds': '0.01', 'node_udp_queues{ip="v4",queue="rx"}': '0', 'node_udp_queues{ip="v4",queue="tx"}': '0', 'node_udp_queues{ip="v6",queue="rx"}': '0', 'node_udp_queues{ip="v6",queue="tx"}': '0', 'node_uname_info{domainname="(none)",machine="x86_64",nodename="hcss-ecs-5402",release="5.15.0-76-generic",sysname="Linux",version="#83-Ubuntu SMP Thu Jun 15 19:16:32 UTC 2023"}': '1', 'node_vmstat_oom_kill': '1', 'node_vmstat_pgfault': '1.67865909e+08', 'node_vmstat_pgmajfault': '29250', 'node_vmstat_pgpgin': '6.454746e+06', 'node_vmstat_pgpgout': '4.6273104e+07', 'node_vmstat_pswpin': '0', 'node_vmstat_pswpout': '0', 'process_cpu_seconds_total': '269.07', 'process_max_fds': '65535', 'process_open_fds': '10', 'process_resident_memory_bytes': '1.4086144e+07', 'process_start_time_seconds': '1.71887336901e+09', 'process_virtual_memory_bytes': '1.271754752e+09', 'process_virtual_memory_max_bytes': '1.8446744073709552e+19', 'promhttp_metric_handler_errors_total{cause="encoding"}': '0', 'promhttp_metric_handler_errors_total{cause="gathering"}': '0', 'promhttp_metric_handler_requests_in_flight': '1', 'promhttp_metric_handler_requests_total{code="200"}': '24666', 'promhttp_metric_handler_requests_total{code="500"}': '0', 'promhttp_metric_handler_requests_total{code="503"}': '0'}



###################################################

# data = "# HELP go_memstats_alloc_bytes Number of bytes allocated and still in use."

# data = "HELP go_memstats_alloc_bytes Number of bytes allocated and still in use."

# for i in data:

# print(i)

# result1 = re.match('[^#]', data, 1)

# # result1 = re.match('^#', data)

# if result1:

# print(data)

 

主机平均负载、内存使用率计算 ——20240625

# 主机平均负载、内存使用

def hum_convert(value):
    units = ["B", "KB", "MB", "GB", "TB", "PB"]
    size = 1024.0
    for i in range(len(units)):
        if (value / size) < 1:
            return "%.2f%s" % (value, units[i])
        value = value / size


print("主机1min、5min、15min平均负载分别为:{0}, {1}, {2}".format(c.get('node_load1'),c.get('node_load5'),c.get('node_load15')))

Mem_total = float(c.get("node_memory_MemTotal_bytes"))
print("主机内存总量:{}  (单位:bytes)".format(Mem_total))
print("主机内存总量:{}  (人性化显示值)".format(hum_convert(Mem_total)))
# 内存使用率计算: ((node_memory_MemTotal_bytes - node_memory_MemFree_bytes - node_memory_Buffers_bytes - node_memory_Cached_bytes) / (node_memory_MemTotal_bytes )) * 100
Mem_used = float(c.get('node_memory_MemFree_bytes'))+float(c.get('node_memory_Buffers_bytes'))+float(c.get('node_memory_Cached_bytes'))
print("主机内存已经使用:",hum_convert(Mem_used))
Memory_usage = (Mem_total - Mem_used) / Mem_total * 100
Memory_usage = round(Memory_usage,2)
print("主机内存使用率:{}%".format(Memory_usage))

 

image.png

 

 

系统运行时间计算,与` uptime -p `显示一致: ___20240625

## node_exporter 系统运行时间计算

import datetime

print("主机启动时间戳:{} (单位:s)".format(float(c.get('node_boot_time_seconds'))))

# print("当前时间戳:", datetime.datetime.now().timestamp())

time2=datetime.datetime.utcfromtimestamp(datetime.datetime.now().timestamp())+datetime.timedelta(hours=8)

time1=datetime.datetime.utcfromtimestamp(float(c.get('node_boot_time_seconds')))+datetime.timedelta(hours=8)

print("当前时间:", time2)

print("主机启动的时间:", time1)

time3 = time2-time1

print("截至目前,主机已经运行的时间:", time3)

image.png

 

 

磁盘分区使用率计算 ——20240625

# 磁盘分区使用率计算 100 - ((node_filesystem_avail_bytes * 100) / node_filesystem_size_bytes )

node_filesystem_avail_bytes = float(c.get('node_filesystem_avail_bytes{device="/dev/vda1",device_error="",fstype="ext4",mountpoint="/"}'))

node_filesystem_size_bytes = float(c.get('node_filesystem_size_bytes{device="/dev/vda1",device_error="",fstype="ext4",mountpoint="/"}'))

print("/分区的总容量为:{}, 可用用容量:{}".format(node_filesystem_size_bytes,node_filesystem_avail_bytes))

print("/分区的总容量为:{}, 可用容容量:{}".format(hum_convert(node_filesystem_size_bytes),hum_convert(node_filesystem_avail_bytes)))

Disk_usage = 100 - ((node_filesystem_avail_bytes * 100) / node_filesystem_size_bytes )

Disk_usage2 = ((node_filesystem_avail_bytes * 100) / node_filesystem_size_bytes )

print("/分区的使用率:{}%".format(round(Disk_usage,2)))

print("/分区未使用率:{}%".format(round(Disk_usage2,2)))

 

image.png

网卡接收、发送的总字节数【还需要看看当前接收/发送的字节数量,每秒的网络速率】

de1=`cat /proc/net/dev | grep -E "eth0" | tr : ' ' | awk '{print $2}'` && sleep 1 && de2=`cat /proc/net/dev | grep -E "eth0" | tr : ' ' | awk '{print $2}'` && echo $de2 - $de1 | bc  #计算1秒ReceiveReceive流量的差值 【while true; do data=$(de1=`cat /proc/net/dev | grep -E 'eth0' | tr : ' ' | awk '{print $2}'` && sleep 1 && de2=`cat /proc/net/dev | grep -E 'eth0' | tr : ' ' | awk '{print $2}'` && echo "scale=3; ($de2 - $de1)/1024"  | bc) ; echo Receive流量$(printf "%.3f" $data)k ;  done】
de1=`cat /proc/net/dev | grep -E "eth0" | tr : ' ' | awk '{print $10}'` && sleep 1 && de2=`cat /proc/net/dev | grep -E "eth0" | tr : ' ' | awk '{print $10}'` && echo $de2 - $de1 | bc #计算1秒Transmit流量的差值  【while true; do data=$(de1=`cat /proc/net/dev | grep -E 'eth0' | tr : ' ' | awk '{print $10}'` && sleep 1 && de2=`cat /proc/net/dev | grep -E 'eth0' | tr : ' ' | awk '{print $10}'` && echo "scale=3; ($de2 - $de1)/1024"  | bc) ; echo Transmit流量$(printf "%.3f" $data)k ;  done
】  ##注意更换网卡名称


# 网卡接收、发送的总字节数
print("网络接收的总字节数:{}  (单位:bytes)".format(c.get('node_network_receive_bytes_total{device="eth0"}')))
print("网络发送的总字节数:{}  (单位:bytes)".format(c.get('node_network_transmit_bytes_total{device="docker0"}')))
print("网络接收的总字节数:{}  (人性化显示值)".format(hum_convert(float(c.get('node_network_receive_bytes_total{device="eth0"}')))))
print("网络发送的总字节数:{}  (人性化显示值)".format(hum_convert(float(c.get('node_network_transmit_bytes_total{device="eth0"}')))))

 

image.png

 

image.png

 

CPU使用率计算【代码有问题,计算的是开机至现在,CPU的使用率】——【思考:___单位时间内的使用率】

CPU使用率 = (所有非空闲状态CPU使用时间总和) / (所有状态CPU时间总和) 【CPU使用时间总和 = 所有非空闲状态CPU使用时间总和 + 所有空闲状态CPU使用时间总和】

==> CPU使用率 = 1 - (所有空闲状态CPU使用时间总和) / (所有状态CPU时间总和)

image.png

 

image.png

# CPU使用时间总和
cpu_total_time0 = (float(c.get('node_cpu_seconds_total{cpu="0",mode="idle"}'))
                   + float(c.get('node_cpu_seconds_total{cpu="0",mode="iowait"}'))
                   + float(c.get('node_cpu_seconds_total{cpu="0",mode="irq"}'))
                   + float(c.get('node_cpu_seconds_total{cpu="0",mode="nice"}'))
                   + float(c.get('node_cpu_seconds_total{cpu="0",mode="softirq"}'))
                   + float(c.get('node_cpu_seconds_total{cpu="0",mode="steal"}'))
                   + float(c.get('node_cpu_seconds_total{cpu="0",mode="system"}'))
                   + float(c.get('node_cpu_seconds_total{cpu="0",mode="user"}')))

cpu_total_time1 = (float(c.get('node_cpu_seconds_total{cpu="1",mode="idle"}'))
                   + float(c.get('node_cpu_seconds_total{cpu="1",mode="iowait"}'))
                   + float(c.get('node_cpu_seconds_total{cpu="1",mode="irq"}'))
                   + float(c.get('node_cpu_seconds_total{cpu="1",mode="nice"}'))
                   + float(c.get('node_cpu_seconds_total{cpu="1",mode="softirq"}'))
                   + float(c.get('node_cpu_seconds_total{cpu="1",mode="steal"}'))
                   + float(c.get('node_cpu_seconds_total{cpu="1",mode="system"}'))
                   + float(c.get('node_cpu_seconds_total{cpu="1",mode="user"}')))

print("cpu0总时间:{}".format(cpu_total_time0))
print("cpu0空闲时间:{}".format(c.get('node_cpu_seconds_total{cpu="0",mode="idle"}')))
print("cpu1总时间:{}".format(cpu_total_time1))
print("cpu1空闲时间:{}".format(c.get('node_cpu_seconds_total{cpu="1",mode="idle"}')), end="\n\n")

print(
    "CPU0的使用率:%.3f%%" % ((1 - float(c.get('node_cpu_seconds_total{cpu="0",mode="idle"}')) / cpu_total_time0) * 100))
print(
    "CPU1的使用率:%.3f%%" % ((1 - float(c.get('node_cpu_seconds_total{cpu="1",mode="idle"}')) / cpu_total_time1) * 100))

count_avg = (((1 - float(c.get('node_cpu_seconds_total{cpu="1",mode="idle"}')) / cpu_total_time1) * 100) + (
            (1 - float(c.get('node_cpu_seconds_total{cpu="0",mode="idle"}')) / cpu_total_time0) * 100)) / 2
print("CPU总使用率:%.3f%%"%count_avg)

 

image.png

 

image.png

 


 

网卡接收、发送的总字节数【还需要看看当前接收/发送的字节数量,每秒的网络速率】注意更换网卡名称

 

de1=`cat /proc/net/dev | grep -E "eth0" | tr : ' ' | awk '{print $2}'` && sleep 1 && de2=`cat /proc/net/dev | grep -E "eth0" | tr : ' ' | awk '{print $2}'` && echo $de2 - $de1 | bc  #计算1秒ReceiveReceive流量的差值 【while true; do data=$(de1=`cat /proc/net/dev | grep -E 'eth0' | tr : ' ' | awk '{print $2}'` && sleep 1 && de2=`cat /proc/net/dev | grep -E 'eth0' | tr : ' ' | awk '{print $2}'` && echo "scale=3; ($de2 - $de1)/1024"  | bc) ; echo Receive流量$(printf "%.3f" $data)k ;  done】
de1=`cat /proc/net/dev | grep -E "eth0" | tr : ' ' | awk '{print $10}'` && sleep 1 && de2=`cat /proc/net/dev | grep -E "eth0" | tr : ' ' | awk '{print $10}'` && echo $de2 - $de1 | bc #计算1秒Transmit流量的差值  【while true; do data=$(de1=`cat /proc/net/dev | grep -E 'eth0' | tr : ' ' | awk '{print $10}'` && sleep 1 && de2=`cat /proc/net/dev | grep -E 'eth0' | tr : ' ' | awk '{print $10}'` && echo "scale=3; ($de2 - $de1)/1024"  | bc) ; echo Transmit流量$(printf "%.3f" $data)k ;  done
】  ##注意更换网卡名称
#!/bin/bash

# 网络速率监控脚本 - 针对您的系统调整
# 用法:./NetworkRate.sh [网络接口名]

# 设置默认网络接口
INTERFACE="eth0"
if [ $# -ge 1 ]; then
    INTERFACE="$1"
fi

# 检查接口是否存在
if ! ip link show "$INTERFACE" > /dev/null 2>&1; then
    echo "错误: 网络接口 $INTERFACE 不存在!"
    echo "可用接口: "
    ip link show | grep -E "^[0-9]+:" | awk -F': ' '{print $2}'
    exit 1
fi

echo "监控网络接口: $INTERFACE"
echo "按 Ctrl+C 停止监控"
echo "----------------------------------------"
printf "%12s %12s %12s\n" "接收速率" "发送速率" "时间"
echo "----------------------------------------"

# 初始化变量
prev_rx=0
prev_tx=0
first_run=1

# 函数:格式化速度显示
format_speed() {
    local bytes=$1
    if [ $bytes -ge 1048576 ]; then
        echo "$(echo "scale=1; $bytes/1048576" | bc)MB/s"
    elif [ $bytes -ge 1024 ]; then
        echo "$(echo "scale=1; $bytes/1024" | bc)KB/s"
    else
        echo "${bytes}B/s"
    fi
}

while true; do
    # 获取当前时间
    current_time=$(date +"%H:%M:%S")
    
    # 获取接口统计信息
    ip_output=$(ip -s link show "$INTERFACE" 2>/dev/null)
    
    # 根据您的输出格式提取接收和发送的字节数
    rx_bytes=$(echo "$ip_output" | grep -A1 "RX:" | tail -1 | awk '{print $1}')
    tx_bytes=$(echo "$ip_output" | grep -A1 "TX:" | tail -1 | awk '{print $1}')
    
    # 检查是否成功获取字节数
    if ! echo "$rx_bytes" | grep -qE '^[0-9]+$' || ! echo "$tx_bytes" | grep -qE '^[0-9]+$'; then
        echo "错误: 无法获取网络统计信息"
        echo "调试信息 - RX: '$rx_bytes', TX: '$tx_bytes'"
        exit 1
    fi
    
    # 如果是第一次循环,只记录值不计算速率
    if [ $first_run -eq 1 ]; then
        prev_rx=$rx_bytes
        prev_tx=$tx_bytes
        first_run=0
        sleep 1
        continue
    fi
    
    # 计算速率 (字节/秒)
    rx_rate=$((rx_bytes - prev_rx))
    tx_rate=$((tx_bytes - prev_tx))
    
    # 转换为更易读的单位
    rx_rate_human=$(format_speed $rx_rate)
    tx_rate_human=$(format_speed $tx_rate)
    
    # 打印结果
    printf "%12s %12s %12s\n" "$rx_rate_human" "$tx_rate_human" "$current_time"
    
    # 更新前一次的值
    prev_rx=$rx_bytes
    prev_tx=$tx_bytes
    
    # 等待1秒
    sleep 1
done

image

 

posted on 2026-01-29 11:51  我,在等待  阅读(3)  评论(0)    收藏  举报