
用R语言统计用户登录信息
#设置统计时间段 格式: 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")
# 筛选统计时间段
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")
#统计每个用户登录时间数
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)")
#统计局域网外的ip登录数
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)
#统计不同用户使用不同ip登录次数
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)
数据分析咨询请扫描二维码
若不方便扫码,搜微信号:CDAshujufenxi
如何考取数据分析师证书:以 CDA 为例 在数字化浪潮席卷各行各业的当下,数据分析师已然成为企业挖掘数据价值、驱动决策的 ...
2025-07-15CDA 精益业务数据分析:驱动企业高效决策的核心引擎 在数字经济时代,企业面临着前所未有的数据洪流,如何从海量数据中提取有 ...
2025-07-15MySQL 无外键关联表的 JOIN 实战:数据整合的灵活之道 在 MySQL 数据库的日常操作中,我们经常会遇到需要整合多张表数据的场景 ...
2025-07-15Python Pandas:数据科学的瑞士军刀 在数据驱动的时代,面对海量、复杂的数据,如何高效地进行处理、分析和挖掘成为关键。 ...
2025-07-15用 SQL 生成逆向回滚 SQL:数据操作的 “后悔药” 指南 在数据库操作中,误删数据、错改字段或误执行批量更新等问题时有发生。 ...
2025-07-14t检验与Wilcoxon检验的选择:何时用t.test,何时用wilcox.test? t 检验与 Wilcoxon 检验的选择:何时用 t.test,何时用 wilcox. ...
2025-07-14AI 浪潮下的生存与进阶: CDA数据分析师—开启新时代职业生涯的钥匙(深度研究报告、发展指导白皮书) 发布机构:CDA数据科 ...
2025-07-13LSTM 模型输入长度选择技巧:提升序列建模效能的关键 在循环神经网络(RNN)家族中,长短期记忆网络(LSTM)凭借其解决长序列 ...
2025-07-11CDA 数据分析师报考条件详解与准备指南 在数据驱动决策的时代浪潮下,CDA 数据分析师认证愈发受到瞩目,成为众多有志投身数 ...
2025-07-11数据透视表中两列相乘合计的实用指南 在数据分析的日常工作中,数据透视表凭借其强大的数据汇总和分析功能,成为了 Excel 用户 ...
2025-07-11尊敬的考生: 您好! 我们诚挚通知您,CDA Level I和 Level II考试大纲将于 2025年7月25日 实施重大更新。 此次更新旨在确保认 ...
2025-07-10BI 大数据分析师:连接数据与业务的价值转化者 在大数据与商业智能(Business Intelligence,简称 BI)深度融合的时代,BI ...
2025-07-10SQL 在预测分析中的应用:从数据查询到趋势预判 在数据驱动决策的时代,预测分析作为挖掘数据潜在价值的核心手段,正被广泛 ...
2025-07-10数据查询结束后:分析师的收尾工作与价值深化 在数据分析的全流程中,“query end”(查询结束)并非工作的终点,而是将数 ...
2025-07-10CDA 数据分析师考试:从报考到取证的全攻略 在数字经济蓬勃发展的今天,数据分析师已成为各行业争抢的核心人才,而 CDA(Certi ...
2025-07-09【CDA干货】单样本趋势性检验:捕捉数据背后的时间轨迹 在数据分析的版图中,单样本趋势性检验如同一位耐心的侦探,专注于从单 ...
2025-07-09year_month数据类型:时间维度的精准切片 在数据的世界里,时间是最不可或缺的维度之一,而year_month数据类型就像一把精准 ...
2025-07-09CDA 备考干货:Python 在数据分析中的核心应用与实战技巧 在 CDA 数据分析师认证考试中,Python 作为数据处理与分析的核心 ...
2025-07-08SPSS 中的 Mann-Kendall 检验:数据趋势与突变分析的有力工具 在数据分析的广袤领域中,准确捕捉数据的趋势变化以及识别 ...
2025-07-08备战 CDA 数据分析师考试:需要多久?如何规划? CDA(Certified Data Analyst)数据分析师认证作为国内权威的数据分析能力认证 ...
2025-07-08