Unix/Linux编程实践教程–ac在Ubuntu 14.04的实现
来源:互联网 发布:单机手机游戏 知乎 编辑:程序博客网 时间:2024/05/17 22:01
环境:Ubuntu 14.04 32位
为什么这回不写在OS x上的实现了?因为OS X使用的是utmpx,然后我用getutxent_wtmp()
这个函数也没有办法正确获取wtmp的日志信息,所以先在Ubuntu上实现好了。
默认没有带ac
这个程序,需要自行安装。(sudo apt-get insall acct
)
好的,开始我们的节目。man ac
,可以看到ac是一个统计用户连接时长(以小时为单位)的工具。用户的登录、退出的信息保存在utmp这个文件中。
那我们就man 5 wtmp
,注意到它的描述里有这么一句话。The wtmp file records all logins and logouts. Its format is exactly like utmp except that a null username indicates a logout on the associated terminal.
这个文件的格式是和utmp类似的,而且如果用户名为空的话,说明用户在对应的终端上退出了。
然后就可以开始尝试写代码了,初版可能是这样的。
/* ubuntu 14.04 */#include <stdio.h>#include <fcntl.h>#include <unistd.h>#include <utmp.h>#include <string.h>void utmpOut(struct utmp *p);int main(void){ int fd; struct utmp utmpBuf; int siz = sizeof(struct utmp); if((fd = open(WTMP_FILE, O_RDONLY)) == -1){ perror("ac_ubuntu"); exit(1); } while(read(fd, &utmpBuf, siz) == siz) utmpOut(&utmpBuf); close(fd); return 0;}void utmpOut(struct utmp *p){ //if(strcmp(p->ut_user, "superxc")) // return ; printf("%d\t%s\t%s\t%s", p->ut_type, p->ut_user, p->ut_line, asctime(localtime(&p->ut_time)));}
运行一下看看wtmp里都写啥了。
1 shutdown ~ Mon May 29 09:11:18 20172 reboot ~ Sun Jun 4 18:46:09 20171 runlevel ~ Sun Jun 4 18:46:09 20176 LOGIN tty4 Sun Jun 4 18:46:09 20176 LOGIN tty5 Sun Jun 4 18:46:09 20176 LOGIN tty2 Sun Jun 4 18:46:09 20176 LOGIN tty3 Sun Jun 4 18:46:09 20176 LOGIN tty6 Sun Jun 4 18:46:09 20176 LOGIN tty1 Sun Jun 4 18:46:09 20177 superxc tty1 Sun Jun 4 18:46:42 20177 superxc pts/0 Sun Jun 4 18:57:52 20177 superxc pts/2 Sun Jun 4 18:58:07 20171 runlevel ~ Sun Jun 4 19:03:16 20178 tty4 Sun Jun 4 19:03:17 20178 tty5 Sun Jun 4 19:03:17 20178 tty2 Sun Jun 4 19:03:17 20178 tty3 Sun Jun 4 19:03:17 20178 tty6 Sun Jun 4 19:03:17 20178 pts/0 Sun Jun 4 19:03:17 20178 pts/2 Sun Jun 4 19:03:17 20171 shutdown ~ Sun Jun 4 19:03:17 20171 shutdown ~ Sun Jun 4 19:03:17 20172 reboot ~ Mon Jun 5 08:21:34 20171 runlevel ~ Mon Jun 5 08:21:34 20176 LOGIN tty4 Mon Jun 5 08:21:34 20176 LOGIN tty5 Mon Jun 5 08:21:34 20176 LOGIN tty2 Mon Jun 5 08:21:34 20176 LOGIN tty3 Mon Jun 5 08:21:34 20176 LOGIN tty6 Mon Jun 5 08:21:34 20176 LOGIN tty1 Mon Jun 5 08:21:34 20177 superxc pts/0 Mon Jun 5 08:22:25 20177 superxc pts/2 Mon Jun 5 08:22:38 20177 root tty1 Mon Jun 5 09:05:05 20178 tty1 Mon Jun 5 09:07:35 20176 LOGIN tty1 Mon Jun 5 09:07:35 2017
我这里只截取了6月4日和6月5日的记录,再拿ac -pd
的记录对比一下。
superxc 0.45Jun 4 total 0.45 superxc 2.47 root 0.04Today total 2.51
这里的Today 指的是6月5日,可以看到6月4日,superxc总共连接了0.45个小时。怎么算的呢?注意看wtmp的输出。关于superxc用户的登录有三条,分别是:
7 superxc tty1 Sun Jun 4 18:46:42 20177 superxc pts/0 Sun Jun 4 18:57:52 20177 superxc pts/2 Sun Jun 4 18:58:07 2017
退出的话,注意刚才文档里写的那句话,也就是我标重点的部分,如果用户名为空的话,说明用户在对应的终端上退出了,所以可以找到下面这两行,就是退出的记录。
8 pts/0 Sun Jun 4 19:03:17 20178 pts/2 Sun Jun 4 19:03:17 2017
这里要注意到tty1没有退出,直接就关机了,所有没有对应的记录。
同一个用户在三个终端登录,时间是怎么算的?由计算得知是累加的。
(63 - 46 + (17 - 42)/60 + /* tty1 */63 - 57 + (17 - 52)/60 + /* pts/0 */63 - 58 + (17 - 07)/60 /* pts/2 */)/60 = 0.45277
刚好和ac的结果一致。
wtmp文件中有些记录是我们并不需要的,ut_type为6、 2、 1的不需要,但是ut_type为1且ty_user为shutdown为需要保留(用于作为关机时没有退出的用户的退出时间)。
所以ac的实现大概是这样的:
Step1: 在ut_type为7的记录中找到第一条未处理的信息、记录下是哪个终端,ut_time记为t1。
Step2: 向后找ut_type为8的记录中与Step1中记录的终端相匹配的(如果遇到到shutdown,就不再向下查找,如果找到EOF,就以当前时间为准),对应的ut_time记为t2。
Step3: t2 - t1的结果累加到sum中。
Step4: 重复Step1-3直到所有记录处理完毕。
所以代码可能是这样的:
/* ubuntu 14.04 */#include <stdio.h>#include <fcntl.h>#include <unistd.h>#include <utmp.h>#include <string.h>#include <time.h>#define UTMPSIZ sizeof(struct utmp)void utmpOut(struct utmp *p);int cntTime(int fd, struct utmp *p);int main(void){ int fd; struct utmp utmpBuf; int sum = 0; /* seconds */ if((fd = open(WTMP_FILE, O_RDONLY)) == -1){ perror("ac_ubuntu"); exit(1); } while(read(fd, &utmpBuf, UTMPSIZ) == UTMPSIZ){ sum += cntTime(fd, &utmpBuf); // utmpOut(&utmpBuf); } close(fd); printf("%d seconds => %f hours\n", sum, sum / (60.0 * 60)); return 0;}void utmpOut(struct utmp *p){ //if(strcmp(p->ut_user, "superxc")) // return ; printf("%d\t%s\t%s\t%s", p->ut_type, p->ut_user, p->ut_line, asctime(localtime(&p->ut_time)));}int cntTime(int fd, struct utmp *p){ char line[256]; struct utmp utmpBuf; int cnt = 0; /* forward how many time */ int flag = 0; int t1, t2; if(p->ut_type != USER_PROCESS) /* USER_PROCESS 7 */ return 0; strcpy(line, p->ut_line); t1 = (int)p->ut_time; /* printf("user %s login on %s at %s", p->ut_user, p->ut_line, asctime(localtime(&p->ut_time))); */ while(read(fd, &utmpBuf, UTMPSIZ) == UTMPSIZ){ ++cnt; /* DEAD_PROCESS 8, RUN_LVL 1 */ /* printf("test record: "); */ /* utmpOut(&utmpBuf); */ if((utmpBuf.ut_type == DEAD_PROCESS && strcmp(line, utmpBuf.ut_line) == 0) || (utmpBuf.ut_type == RUN_LVL && strcmp("shutdown", utmpBuf.ut_user) == 0)){ flag = 1; /* printf("find!\n"); */ break; } } t2 = flag ? utmpBuf.ut_time : (int)time(NULL); /* if(flag){ printf("log out at %s", asctime(localtime(&utmpBuf.ut_time))); }else{ printf("log out at now\n"); } printf(" tabkes %d seconds\n", t2 - t1); */ lseek(fd, -cnt * UTMPSIZ, SEEK_CUR); /* roll back */ return t2 - t1;}
代码中使用了lseek进行回滚指针,本程序并没有使用缓存,有兴趣的可以自己加上,还有比如-p
、-d
之类的选项参数都可以加。
执行效果:
有个大犇的实现做得比我好多了,可以很大程序减少系统调用,时间开销比较小,而且能结合缓冲,可扩展性也挺好。链接
- Unix/Linux编程实践教程–ac在Ubuntu 14.04的实现
- Unix/Linux编程实践教程–last在Ubuntu 14.04的实现
- Unix/Linux编程实践教程–cp在OS X的实现
- Unix/Linux编程实践教程–cat在OS X的实现
- Unix/Linux编程实践教程–head在OS X的实现
- Unix/Linux编程实践教程–tail在OS X的实现
- Unix/Linux编程实践教程–od在OS X的实现
- Unix/Linux编程实践教程–chmod在Centos7.3的实现
- Unix/Linux编程实践教程--who在OS X的实现
- Unix/Linux编程实践教程–书评
- Unix/Linux编程实践教程
- Unix/Linux编程实践教程
- Unix-linux编程实践教程
- unix/linux编程实践教程---sscanf函数的用法
- 《unix/linux编程实践教程》之管道
- unix/linux编程实践教程--more命令
- unix/linux编程实践教程:who命令
- unix/linux编程实践教程:ls命令
- VC 6.0配置parseGen
- Maven项目中获取classpath和资源文件的路径
- NOLO今日登陆上海CES:完美运行Steam VR的国产神器
- MongoDB DBRef和手动引用(Manual References)
- git使用时出错
- Unix/Linux编程实践教程–ac在Ubuntu 14.04的实现
- tomcat开启二级域名访问设置
- 【C#】深度理解虚方法 继承 以及接口的简单使用
- Eclipse+Maven+Tomcat+JRebel实现项目的热部署
- python numpy ndarray astype error
- linux 常用脚本
- 织梦cms出现DedeTag Engine Create File False的解决
- Linux系统重启network服务失败
- fastJson中 JSON.toJSONString(list,true) 和 JSON.toJSONString(list)的区别