在 gnuplot 中繪製時間數據

在 gnuplot 中繪製時間數據

我有一個日誌檔案(auth.log),其中不相關的行已被刪除。我希望將每小時/每天的線聚合到圖中,這意味著同一小時或同一天內的每條線​​都聚合到圖中的一個抽動中。

我一直在研究函數,但我一直陷入困境。

這是我到目前為止所擁有的,但只有當我在日誌檔案中的每一行都有一個「變數」時它才會起作用。

#!/usr/bin/env gnuplot                                                          

set terminal png size 1200,800                                                  
set output "graph.png"                                                          
set title "Breakin Attempts"                                                    

set key top right box                                                           
set style data lines                                                            
set border 3                                                                    
set grid                                                                        
set pointsize 3                                                                 

set xlabel "Number of breakin attempts"                                         
set xtics nomirror                                                              
set xdata time                                                                  
set timefmt "%b %d %H:%M:%S"                                                    
set format x "%m/%d"                                                            

set ylabel "Time"                                                               
set ytics nomirror                                                              

plot "pc1.log" using 1:4 title "PC1" linecolor rgb "red", \                                                  
     "pc2.log" using 1:4 title "PC2" linecolor rgb "blue", \            
     "pc3.log" using 1:4 title "PC3" linecolor rgb "green"

這是數據的範例

Sep 18 11:26:30 root 60.191.36.196                                              
Sep 18 11:26:34 root 60.191.36.196                                              
Sep 18 11:26:37 root 60.191.36.196
Sep 18 19:21:31 root 198.56.193.74                                              
Sep 18 19:21:33 root 198.56.193.74

在這種情況下,19:21:xx 處的兩個條目將是 2 的 1 個 tic,11:26:xx 處的 3 個條目將是 3 的 tic。

答案1

我假設您想要每個時間單位的條目數(示例中為分鐘)。我不知道 gnuplot 是否可以用這種方式計算行數。我會使用awk(或任何對您方便的語言)來累積資料。像這樣的事情會做:

腳本='{時間=$3; gsub(/:[0-9][0-9]$/, "", 時間); date=sprintf("%s %s %s", $1, $2, time)} date==last{count++} date!=last{列印日期, count;計數=0}'

管道(檔案)= sprintf(“< awk '%s'%s”,腳本,檔案)情節管道(“pc1.log”)標題“PC1”

答案2

你的問題不是很明確。作為 Hannes,我假設您想要繪製與特定日期相對應的行數。

Gnuplot 不太適合於此,建議對檔案進行預處理。

但是,使用 gnuplot 3.4 或更高版本,您可以對計數器進行程式設計(作為全域變數),因此您可以獲得如下所示的內容:

currentx=1/0
currentn=0
increaseandreturn(returnvalue)=(currentn=currentn+1,returnvalue)
startnewxandreturn(x,returnvalue)=(currentx=x,currentn=0,returnvalue)
count(x)=((x==currentx)?increaseandreturn(1/0):startnewxandreturn(x,currentn))
plot "file.gdat" using ($1-1):(count($1)) with points

它僅適用於排序的文件(它將添加連續的條目,而不是不連續的條目),currentx必須包含第一個值(或您需要插入更多測試)。對於日期,您需要稍微調整腳本。

您可以使用 gnuplot 產生的檔案來測試它,如下所示:

set table "file.gdat"
set parametric
plot [0:20] floor(exp(t/10)),t
unset table

相關內容