iT邦幫忙

0

網路流量紀錄。可以記錄到每分鐘

請問網路流量紀錄。可以記錄到每分鐘
我有測試過NetWorx,WatchLan似乎都只能記錄每日,保留記錄3天內
而我想要記錄到每分鐘,這是因為我們有一個設備類似WebCam,是直接透過網路線傳輸影像到電腦
但常常在半夜設備系統會當機,設備商說有可能是因為網路傳輸訊號的問題(異常),所以想找可每分鐘紀錄網路流量的軟體,來判斷當機前後幾分鐘是否網路流量瞬間過大,進而影響系統或設備傳輸訊號


謝謝一級屠豬士的指教,我是根據一級屠豬士的提示,用C#方式寫出來的,因為我沒學過Python
,但我這種小程式,實在不敢發表什麼技術文件,所以把程式都貼在下面,(大多程式碼都是網路搜尋後結合的)
功能:每天產生一個年月日.txt的文件檔,然後每5秒計算一次流量,寫入文件檔

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.NetworkInformation;
using System.IO;

namespace NetTrafficCmd
{
    class Program
    {
        static void Main(string[] args)
        {
            long packetsCurrentLong = 0;
            long packetsDeliveredCurrentLong = 0;
            long packetsBefLong = 0;
            long packetsDeliveredBefLong = 0;

            while (true)
            {

                // 取和設置當前目錄的完全限定路徑。每日產生一個檔名為年月日的txt檔
                string currentPathStr = System.Environment.CurrentDirectory;
                string fileNmStr = DateTime.Now.ToString("yyyyMMdd") + ".txt";

                // 判斷 test.txt 檔案是否存在
                if (System.IO.File.Exists(fileNmStr))
                {
                    //MessageBox.Show(FileName + " 檔案存在");
                }
                else
                {
                    //建立文件本
                    FileStream fileStream = new FileStream(currentPathStr + '\\' + fileNmStr, FileMode.Create);

                    fileStream.Close();   //切記開了要關,不然會被佔用而無法修改喔!!!
                }

                IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
                IPGlobalStatistics ipstat = properties.GetIPv4GlobalStatistics();

                packetsCurrentLong = ipstat.ReceivedPackets;
                packetsDeliveredCurrentLong = ipstat.ReceivedPacketsDelivered;

                if (packetsBefLong == 0) {
                    packetsBefLong = packetsCurrentLong;
                }

                if (packetsDeliveredBefLong == 0)
                {
                    packetsDeliveredBefLong = packetsDeliveredCurrentLong;
                }

                Console.Clear();
                Console.WriteLine(DateTime.Now.ToString("yyyyMMdd HH:mm:ss"));
                Console.WriteLine("*接收封包數:" + (packetsCurrentLong - packetsBefLong) );
                Console.WriteLine("*傳送封包數:" + (packetsDeliveredCurrentLong - packetsDeliveredBefLong) );
                Console.WriteLine("接收封包數:" + ipstat.ReceivedPackets);
                Console.WriteLine("傳送封包數:" + ipstat.ReceivedPacketsDelivered);
                //Console.WriteLine("轉發封包數:" + ipstat.ReceivedPacketsForwarded);
                //Console.WriteLine("丟棄封包數:" + ipstat.ReceivedPacketsDiscarded);
                System.Threading.Thread.Sleep(5000);//停留五秒後繼續,(每5秒算一次流量)

                using (StreamWriter sw = new StreamWriter(currentPathStr + '\\' + fileNmStr, true))
                {
                    // 欲寫入的文字資料 ~
                    sw.WriteLine(DateTime.Now.ToString("yyyyMMdd HH:mm:ss") + "\t" + (packetsCurrentLong - packetsBefLong).ToString() + "\t" + (packetsDeliveredCurrentLong - packetsDeliveredBefLong).ToString());
                }

                packetsBefLong = packetsCurrentLong;
                packetsDeliveredBefLong = packetsDeliveredCurrentLong;

            }
        }
    }
}
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

4
一級屠豬士
iT邦大師 1 級 ‧ 2020-11-17 11:44:35
最佳解答

我在8年前寫的,用 Python 抓網路流量,繪製RRD的圖.

https://ithelp.ithome.com.tw/articles/10105769

https://ithelp.ithome.com.tw/articles/10106043

https://ithelp.ithome.com.tw/articles/10106323

是每分鐘抓的,記得使用 cron 設定時,要用每分鐘的格式.

你可以自己加上 SNMP 的方式, 就能抓網路設備的.

看更多先前的回應...收起先前的回應...
jhit03 iT邦新手 4 級 ‧ 2020-11-17 14:18:11 檢舉

感謝大大,
1.因為我不需要圖,我只需要紀錄
2020/11/17 13:00 流量XX
2020/11/17 13:01 流量XX
2020/11/17 13:02 流量XX
,類似這樣紀錄在記事本txt裡面
是不是只要拿你第一個連結的程式碼來修改,加上寫入txt檔就可以了

你若是要抓網路設備,那就是要透過 SNMP.
不管是遠端網路設備,或是本機的網路卡,實際上都是有 counter,
就是紀錄目前收發的封包以及bytes,還有錯誤,共6個.
流量實際上是用 兩次取樣的差距 / 時間 來換算出來的.
因為RRD會幫我換算,你要自己算,那就可能要自己設法.
有些是用 0.1秒的差距 來算出那時候的流量.
建議你可以先試試看自己開發,雖然會辛苦一點,但是會有意思.

jhit03 iT邦新手 4 級 ‧ 2020-11-17 17:16:27 檢舉

感謝大大的方向,我已經開發出來了,當然一大半是一級屠豬士的功勞,讚讚

太好了,恭喜你! 若可以的話,建議你把成果整理一下,發表技術文章,
跟大家分享.

0
XZK
iT邦新手 4 級 ‧ 2020-11-20 13:32:32

Prometheus + Blackbox expoter + Grafana

我要發表回答

立即登入回答