
用R語言統(tǒng)計(jì)用戶登錄信息
#設(shè)置統(tǒng)計(jì)時(shí)間段 格式: yyyy-mm-dd-hh-mm-ss
#如: 2013-09-10-00-00-00
start.time = "2013-09-01-00-00-00"
stop.time = "2013-10-13-00-00-00"
#偽裝用戶名
fake = T
library(lattice)
file = "login.log"
lines = readLines(con=file)
data = NULL
in.time = NULL
ch2d = function(x=NULL)
{
if(x=="Jan") return(1)
if(x=="Feb") return(2)
if(x=="Mar") return(3)
if(x=="Apr") return(4)
if(x=="May") return(5)
if(x=="Jun") return(6)
if(x=="Jul") return(7)
if(x=="Aug") return(8)
if(x=="Sep") return(9)
if(x=="Oct") return(10)
if(x=="Nov") return(11)
if(x=="Dec") return(12)
}
for( i in lines)
{
if( grepl(pattern="still",x=i)
| grepl(pattern="reboot",x=i)
| grepl(pattern="tty",x=i)
| grepl(pattern="wtmp",x=i)
| grepl(pattern="crash",x=i)
| grepl(pattern="down",x=i)
| i=="")
next
tmp = unlist(strsplit(x=i,split=" "))
tmp = tmp[tmp!=""]
tmp[10] = gsub(pattern="\\(",replacement="",x=tmp[10])
tmp[10] = gsub(pattern="\\)",replacement="",x=tmp[10])
tmp = c(tmp,unlist(strsplit(x=tmp[7],split=":")),unlist(strsplit(x=tmp[9],split=":")))
if(length(unlist(strsplit(x=tmp[10],split="\\+")))==2)
{ http://cda.pinggu.org/view/4496.html
day = unlist(strsplit(x=tmp[10],split="\\+"))[1]
tmp[10] = unlist(strsplit(x=tmp[10],split="\\+"))[2]
} else day = 0
hour = unlist(strsplit(x=tmp[10],split=":"))[1]
min = unlist(strsplit(x=tmp[10],split=":"))[2]
time = as.numeric(day) * 24 * 60 + as.numeric(hour) * 60 + as.numeric(min)
in.time = c(in.time,time)
rm(time)
data = rbind(data,tmp)
}
login.time = ISOdatetime(year=2013,month=lapply(X=data[,5],FUN=ch2d),day=data[,6],hour=data[,11],min=data[,12],sec=0)
rownames(data) = 1:nrow(data)
data = data.frame(data[,c(1,3:6,11,12)],in.time)
colnames(data) = c("user","IP","week","month","day","hour","min","time")
# 篩選統(tǒng)計(jì)時(shí)間段
start.time = as.numeric(unlist(strsplit(x=start.time,split="-")))
stop.time = as.numeric(unlist(strsplit(x=stop.time,split="-")))
start.time = ISOdatetime(year=start.time[1],month=start.time[2],
day=start.time[3],hour=start.time[4],
min=start.time[5],sec=start.time[6])
stop.time = ISOdatetime(year=stop.time[1],month=stop.time[2],
day=stop.time[3],hour=stop.time[4],
min=stop.time[5],sec=stop.time[6])
data = data[login.time>=start.time&login.time<=stop.time,]
print(paste(nrow(data),"records after filter."),quote=F)
#偽裝用戶名
if( fake == T )
{
# fake.name = matrix(sample(100:120,length(levels(data$user))*9,replace=T),ncol=9)
# fake.name = apply(fake.name,1,function(x)paste(intToChar(x),collapse=""))
fake.name = rep("",length=nrow(data))
for( i in unique(data$user))
{
fake.name[data$user==i] = paste(intToChar(sample(100:120,9)),collapse="")
}
data = cbind(data,fake.name)
data = data[,c(9,2:8)]
}
colnames(data) = c("user","IP","week","month","day","hour","min","time")
#統(tǒng)計(jì)每個(gè)用戶登錄時(shí)間數(shù)
time.per.user = data.frame(user=character(),time=numeric())
for( i in unique(data$user))
{
time.per.user = rbind(time.per.user,data.frame(user=i,time=sum(in.time[data$user==i])))
}
time.per.user = time.per.user[order(time.per.user$time),]
tp1 = barchart(time~user,time.per.user,scale=list(x=list(rot=90)),ylab="Time(minutes)")
#統(tǒng)計(jì)局域網(wǎng)外的ip登錄數(shù)
IP.info = data$IP
IP.info = IP.info[!grepl(pattern="^10",x=IP.info)]
IP.info = IP.info[!grepl(pattern="^192",x=IP.info)]
IP.info = IP.info[!grepl(pattern="cu",x=IP.info)]
IP.info = IP.info[!grepl(pattern="io",x=IP.info)]
IP.info = IP.info[!grepl(pattern=":",x=IP.info)]
print(paste(" IP counts :",length(unique(IP.info))),quote=F)
#統(tǒng)計(jì)不同用戶使用不同ip登錄次數(shù)
tp2 = histogram(IP~user,data,scales=list(x=list(rot=90)))
plot(tp1,split=c(1,1,1,2))
plot(tp2,split=c(1,2,1,2),new=F)
數(shù)據(jù)分析咨詢請(qǐng)掃描二維碼
若不方便掃碼,搜微信號(hào):CDAshujufenxi
AI 浪潮下的生存與進(jìn)階: CDA數(shù)據(jù)分析師—開啟新時(shí)代職業(yè)生涯的鑰匙(深度研究報(bào)告、發(fā)展指導(dǎo)白皮書) 發(fā)布機(jī)構(gòu):CDA數(shù)據(jù)科 ...
2025-07-13LSTM 模型輸入長度選擇技巧:提升序列建模效能的關(guān)鍵? 在循環(huán)神經(jīng)網(wǎng)絡(luò)(RNN)家族中,長短期記憶網(wǎng)絡(luò)(LSTM)憑借其解決長序列 ...
2025-07-11CDA 數(shù)據(jù)分析師報(bào)考條件詳解與準(zhǔn)備指南? ? 在數(shù)據(jù)驅(qū)動(dòng)決策的時(shí)代浪潮下,CDA 數(shù)據(jù)分析師認(rèn)證愈發(fā)受到矚目,成為眾多有志投身數(shù) ...
2025-07-11數(shù)據(jù)透視表中兩列相乘合計(jì)的實(shí)用指南? 在數(shù)據(jù)分析的日常工作中,數(shù)據(jù)透視表憑借其強(qiáng)大的數(shù)據(jù)匯總和分析功能,成為了 Excel 用戶 ...
2025-07-11尊敬的考生: 您好! 我們誠摯通知您,CDA Level I和 Level II考試大綱將于 2025年7月25日 實(shí)施重大更新。 此次更新旨在確保認(rèn) ...
2025-07-10BI 大數(shù)據(jù)分析師:連接數(shù)據(jù)與業(yè)務(wù)的價(jià)值轉(zhuǎn)化者? ? 在大數(shù)據(jù)與商業(yè)智能(Business Intelligence,簡稱 BI)深度融合的時(shí)代,BI ...
2025-07-10SQL 在預(yù)測分析中的應(yīng)用:從數(shù)據(jù)查詢到趨勢預(yù)判? ? 在數(shù)據(jù)驅(qū)動(dòng)決策的時(shí)代,預(yù)測分析作為挖掘數(shù)據(jù)潛在價(jià)值的核心手段,正被廣泛 ...
2025-07-10數(shù)據(jù)查詢結(jié)束后:分析師的收尾工作與價(jià)值深化? ? 在數(shù)據(jù)分析的全流程中,“query end”(查詢結(jié)束)并非工作的終點(diǎn),而是將數(shù) ...
2025-07-10CDA 數(shù)據(jù)分析師考試:從報(bào)考到取證的全攻略? 在數(shù)字經(jīng)濟(jì)蓬勃發(fā)展的今天,數(shù)據(jù)分析師已成為各行業(yè)爭搶的核心人才,而 CDA(Certi ...
2025-07-09【CDA干貨】單樣本趨勢性檢驗(yàn):捕捉數(shù)據(jù)背后的時(shí)間軌跡? 在數(shù)據(jù)分析的版圖中,單樣本趨勢性檢驗(yàn)如同一位耐心的偵探,專注于從單 ...
2025-07-09year_month數(shù)據(jù)類型:時(shí)間維度的精準(zhǔn)切片? ? 在數(shù)據(jù)的世界里,時(shí)間是最不可或缺的維度之一,而year_month數(shù)據(jù)類型就像一把精準(zhǔn) ...
2025-07-09CDA 備考干貨:Python 在數(shù)據(jù)分析中的核心應(yīng)用與實(shí)戰(zhàn)技巧? ? 在 CDA 數(shù)據(jù)分析師認(rèn)證考試中,Python 作為數(shù)據(jù)處理與分析的核心 ...
2025-07-08SPSS 中的 Mann-Kendall 檢驗(yàn):數(shù)據(jù)趨勢與突變分析的有力工具? ? ? 在數(shù)據(jù)分析的廣袤領(lǐng)域中,準(zhǔn)確捕捉數(shù)據(jù)的趨勢變化以及識(shí)別 ...
2025-07-08備戰(zhàn) CDA 數(shù)據(jù)分析師考試:需要多久?如何規(guī)劃? CDA(Certified Data Analyst)數(shù)據(jù)分析師認(rèn)證作為國內(nèi)權(quán)威的數(shù)據(jù)分析能力認(rèn)證 ...
2025-07-08LSTM 輸出不確定的成因、影響與應(yīng)對(duì)策略? 長短期記憶網(wǎng)絡(luò)(LSTM)作為循環(huán)神經(jīng)網(wǎng)絡(luò)(RNN)的一種變體,憑借獨(dú)特的門控機(jī)制,在 ...
2025-07-07統(tǒng)計(jì)學(xué)方法在市場調(diào)研數(shù)據(jù)中的深度應(yīng)用? 市場調(diào)研是企業(yè)洞察市場動(dòng)態(tài)、了解消費(fèi)者需求的重要途徑,而統(tǒng)計(jì)學(xué)方法則是市場調(diào)研數(shù) ...
2025-07-07CDA數(shù)據(jù)分析師證書考試全攻略? 在數(shù)字化浪潮席卷全球的當(dāng)下,數(shù)據(jù)已成為企業(yè)決策、行業(yè)發(fā)展的核心驅(qū)動(dòng)力,數(shù)據(jù)分析師也因此成為 ...
2025-07-07剖析 CDA 數(shù)據(jù)分析師考試題型:解鎖高效備考與答題策略? CDA(Certified Data Analyst)數(shù)據(jù)分析師考試作為衡量數(shù)據(jù)專業(yè)能力的 ...
2025-07-04SQL Server 字符串截取轉(zhuǎn)日期:解鎖數(shù)據(jù)處理的關(guān)鍵技能? 在數(shù)據(jù)處理與分析工作中,數(shù)據(jù)格式的規(guī)范性是保證后續(xù)分析準(zhǔn)確性的基礎(chǔ) ...
2025-07-04CDA 數(shù)據(jù)分析師視角:從數(shù)據(jù)迷霧中探尋商業(yè)真相? 在數(shù)字化浪潮席卷全球的今天,數(shù)據(jù)已成為企業(yè)決策的核心驅(qū)動(dòng)力,CDA(Certifie ...
2025-07-04