正则表达式的脚本代码地址:https://github.com/Rtoax/test/tree/master/regular-expression/ps-time
C语言完整代码:https://github.com/Rtoax/test/blob/master/regular-expression/ps-time/getprocessruntime-regex.c
问题描述
首先我们使用ps指令的功能:
ps -eo pid,etime,cmd | grep [pname] | grep [pid] | awk '{print $2}'
如下图,我们获得的结果像下面这样
143477 04:19:27 [kworker/0:0]
180337 02:00:10 vpp
181973 39:47 pickup -l -t unix -u
182845 02:15 [kworker/1:2]
182855 00:00 ps -eo pid,etime,cmd
197064 27-08:19:50 /usr/sbin/crond -n
197218 27-08:19:04 /sbin/auditd
通过使用awk过滤掉多余的信息后,发现,时间格式如下:
39:47 分钟:秒
02:00:10 小时:分钟:秒
27-08:19:50 天:小时:分钟:秒
解决方案
正则表达式的脚本代码地址:https://github.com/Rtoax/test/tree/master/regular-expression/ps-time
怎么解呢,没错,当然是正则表达式,于是我写了一个格式的文件如下
[root@localhost ps-time]# cat time.txt
0-12:14:40
1-0:1:0
1-0:13:00
1-00:1:00
1-02:13:00
12-02:12:55
2-02:12:55
1234-2:12:55
1:0:1
2:13:00
12:3:00
1:1:19
02:13:00
0:0
11:1
1:12
59:59
脚本呢,如下:
#!/bin/bash
# 匹配 00:00 类字符串
echo ">> 匹配 00:00 类字符串"
cat time.txt | egrep "^(([0-9]|[0-9]\\w+)\\:([0-9]|[0-9]\\w+)){1,1}$"
# 匹配 00:00:00 类字符串
echo ">> 匹配 00:00:00 类字符串"
cat time.txt | egrep "^(([0-9]|[0-9]\\w+)\\:){2,2}([0-9]|[0-9]\\w+)$"
# 匹配 00-00:00:00 类字符串
echo ">> 匹配 00-00:00:00 类字符串"
cat time.txt | egrep "^([0-9]|[0-9]\\w+)\\-(([0-9]|[0-9]\\w+)\\:){2,2}[0-9]\\w+$"
运行结果如下:
[root@localhost ps-time]# sh regex.sh
>> 匹配 00:00 类字符串
0:0
11:1
1:12
59:59
>> 匹配 00:00:00 类字符串
1:0:1
2:13:00
12:3:00
1:1:19
02:13:00
>> 匹配 00-00:00:00 类字符串
0-12:14:40
1-0:13:00
1-00:1:00
1-02:13:00
12-02:12:55
2-02:12:55
1234-2:12:55
可以说匹配完全正确。
C语言实现思路
下面就可以说手编写C语言代码了,定义结构和枚举
struct timerun {
int tm_sec;
int tm_min;
int tm_hour;
int tm_day;
int tm_year;
};
typedef enum {
__TIME_PATTERN_MS = 0, //00:00
__TIME_PATTERN_HMS = 1, //00:00:00
__TIME_PATTERN_DHMS = 2, //00-00:00:00
__TIME_PATTERN_NOTMATCH = 3, //不匹配
}__TIME_PATTERN;
定义一个正则表达式的pattern结构
static const char * const time_regex_pattern[] = {
"^(([0-9]|[0-9]\\w+)\\:([0-9]|[0-9]\\w+)){1,1}$", //00:00
"^(([0-9]|[0-9]\\w+)\\:){2,2}([0-9]|[0-9]\\w+)$", //00:00:00
"^([0-9]|([0-9]\\w+))\\-(([0-9]|[0-9]\\w+)\\:){2,2}[0-9]\\w+$", //00-00:00:00
};
如何解析这个时间结构呢,我采用struct tm结构解析,当然使用strptime函数解析。
上文提到的,这三种字符串
39:47 分钟:秒
02:00:10 小时:分钟:秒
27-08:19:50 天:小时:分钟:秒
若采用strptime解析的话,需要对应的字符串匹配,我将其定义为:
static const char * const time_strptime_pattern[] = {
"%M:%S", //00:00
"%H:%M:%S", //00:00:00
"%j-%H:%M:%S", //00-00:00:00
};
正则表达式如何实现
/* */
static __TIME_PATTERN match_pattern(const char *str)
{
static const char * const time_regex_pattern[] = {
"^(([0-9]|[0-9]\\w+)\\:([0-9]|[0-9]\\w+)){1,1}$", //00:00
"^(([0-9]|[0-9]\\w+)\\:){2,2}([0-9]|[0-9]\\w+)$", //00:00:00
"^([0-9]|([0-9]\\w+))\\-(([0-9]|[0-9]\\w+)\\:){2,2}[0-9]\\w+$", //00-00:00:00
};
int status = 0, i = 0;
int flag = REG_EXTENDED, ipattern = __TIME_PATTERN_MS;
regmatch_t pmatch[1];
const size_t nmatch = 1;
regex_t reg;
do {
regcomp(®, time_regex_pattern[ipattern], flag);
status = regexec(®, str, nmatch, pmatch, 0);
if(status == REG_NOMATCH){
ipattern++;
}else if(status == 0){
regfree(®);
break;
}
if(ipattern == __TIME_PATTERN_NOTMATCH) {
regfree(®);
break;
}
}while(1);
return ipattern;
}
完整的代码
C语言完整代码:https://github.com/Rtoax/test/blob/master/regular-expression/ps-time/getprocessruntime-regex.c
#include <stdio.h>
#include <time.h>
#include <sys/types.h>
#include <regex.h>
#include <malloc.h>
#include <string.h>
#include <fcntl.h>
struct timerun {
int tm_sec;
int tm_min;
int tm_hour;
int tm_day;
int tm_year;
};
typedef enum {
__TIME_PATTERN_MS = 0, //00:00
__TIME_PATTERN_HMS = 1, //00:00:00
__TIME_PATTERN_DHMS = 2, //00-00:00:00
__TIME_PATTERN_NOTMATCH = 3, //不匹配
}__TIME_PATTERN;
static int parse_time_string(__TIME_PATTERN time_pattern, const char *time_str, struct timerun *tr)
{
if(time_pattern != __TIME_PATTERN_MS &&
time_pattern != __TIME_PATTERN_HMS &&
time_pattern != __TIME_PATTERN_DHMS
)
{
return -1;
}
if(!time_str) return -1;
static const char * const time_strptime_pattern[] = {
"%M:%S", //00:00
"%H:%M:%S", //00:00:00
"%j-%H:%M:%S", //00-00:00:00
};
struct tm t4;
memset(&t4, 0, sizeof(struct tm));
memset(tr, 0, sizeof(struct timerun));
strptime(time_str, time_strptime_pattern[time_pattern], &t4);
tr->tm_year = 0;
tr->tm_day = t4.tm_yday?t4.tm_yday+1:0;
tr->tm_hour = t4.tm_hour;
tr->tm_min = t4.tm_min;
tr->tm_sec = t4.tm_sec;
return 0;
}
/* */
static __TIME_PATTERN match_pattern(const char *str)
{
static const char * const time_regex_pattern[] = {
"^(([0-9]|[0-9]\\w+)\\:([0-9]|[0-9]\\w+)){1,1}$", //00:00
"^(([0-9]|[0-9]\\w+)\\:){2,2}([0-9]|[0-9]\\w+)$", //00:00:00
"^([0-9]|([0-9]\\w+))\\-(([0-9]|[0-9]\\w+)\\:){2,2}[0-9]\\w+$", //00-00:00:00
};
int status = 0, i = 0;
int flag = REG_EXTENDED, ipattern = __TIME_PATTERN_MS;
regmatch_t pmatch[1];
const size_t nmatch = 1;
regex_t reg;
do {
regcomp(®, time_regex_pattern[ipattern], flag);
status = regexec(®, str, nmatch, pmatch, 0);
if(status == REG_NOMATCH){
ipattern++;
}else if(status == 0){
regfree(®);
break;
}
if(ipattern == __TIME_PATTERN_NOTMATCH) {
regfree(®);
break;
}
}while(1);
return ipattern;
}
static int test_regex()
{
char timestring[32] = {0};
int day, hour, min, sec;
for(day=4;day<=99;day+=5) {
for(hour=0;hour<=23;hour+=5) {
for(min=0;min<=59;min+=5) {
for(sec=0;sec<=59;sec+=5) {
memset(timestring, 0, sizeof(timestring));
switch((day+hour+min+sec)%3) {
case 0:
sprintf(timestring, "%d-%02d:%02d:%02d", day, hour, min, sec);
break;
case 1:
sprintf(timestring, "%02d:%02d:%02d", hour, min, sec);
break;
case 2:
sprintf(timestring, "%02d:%02d", min, sec);
break;
}
int pattern = match_pattern(timestring);
printf("%32s pattern = %d\n", timestring, pattern);
}
}
}
}
}
int get_process_runtime(int pid, const char *pname, struct timerun *tr)
{
char cmd[256] = {0};
char buffer[256] = {0};
if(pname) {
sprintf(cmd, "ps -eo pid,etime,cmd | grep %s | grep %d | awk '{print $2}'", pname, pid);
} else {
sprintf(cmd, "ps -eo pid,etime,cmd | grep %d | awk '{print $2}'", pid);
}
FILE *fpcmd = popen(cmd, "r");
fgets(buffer, sizeof(buffer), fpcmd);
buffer[strlen(buffer)-1] = '\0';
int pattern = match_pattern(buffer);
parse_time_string(pattern, buffer, tr);
pclose(fpcmd);
return 0;
}
int main(int argc, char *argv[])
{
struct timerun tr;
while(1) {
sleep(1);
get_process_runtime(getpid(), argv[0], &tr);
get_process_runtime(1, "systemd", &tr);
printf("running time %d-%d:%d:%d\n", tr.tm_day, tr.tm_hour, tr.tm_min, tr.tm_sec);
}
}
使用方法
程序中获取系统的守护进程systemd(systemd是1号进程)的运行时间,如下
get_process_runtime(1, "systemd", &tr);
printf("running time %d-%d:%d:%d\n", tr.tm_day, tr.tm_hour, tr.tm_min, tr.tm_sec);
结果如下:
[root@localhost ps-time]# ./a.out
running time 49-4:2:3
running time 49-4:2:5
running time 49-4:2:6
^C
与直接用指令查出来的一致
[root@localhost ps-time]# ps -eo pid,etime,cmd | grep systemd
1 49-04:03:54 /usr/lib/systemd/systemd --system --deserialize 20
499 49-04:03:52 /usr/lib/systemd/systemd-journald
549 49-04:03:51 /usr/lib/systemd/systemd-udevd
1367 49-04:03:48 /usr/lib/systemd/systemd-machined
1386 49-04:03:48 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation
1434 49-04:03:48 /usr/lib/systemd/systemd-logind
183172 00:00 grep --color=auto systemd