[英]Script for printing out the average number of users logged into the system in the same period of time in a month
I need to write a script, which prints out the average number of users logged into the system in the same time period in a month. 我需要编写一个脚本,该脚本打印出一个月内同一时间段内登录系统的平均用户数。 I need to use 'last' and 'awk'.
我需要使用'last'和'awk'。
To make it more clear, let's take for example March and 10:00-11:00 as a time period. 为了更清楚,让我们以3月和10:00-11:00作为时间段。 Let's suppose on 1st of March we had 2 users logged in that period, on the 2nd of March we had 4 users logged in that period and so on.
让我们假设在3月1日我们有2个用户登录那个时期,3月2日我们有4个用户登录那个时期,依此类推。 For those 2 days we have as an average number.
在这2天中,我们有平均数。
This is what i got so far, but i only managed to display the users logged in a date given. 这是我到目前为止所得到的,但我只设法显示登录日期的用户。
if [ $# = 0 ]
then
echo "Give more parameters"
else
echo "Date:"
d=`read`
for i
do
echo "user: $i"
last $i | grep $d
done
fi
I am expecting to see as a result something like this: 我希望看到这样的结果:
In March the average number of users between 10:00 and 11:00 was x. 3月份,10:00到11:00之间的平均用户数为x。 (just as an example of output expected)
(仅作为预期输出的一个例子)
Something of this sort : 这种东西:
last | awk '{
if(NF>7){
if($(NF-5)=="Mar"){ # You may change the month or even accept it as a parameter
if($(NF-4) != date){datecount++};
match($(NF-3),/^([0-9]+)/,arr);
if(arr[1]>=8 && arr[1]<=16){
# Note I am using different time interval, change it accordingly
count++;
}
}
date=$(NF-4);
}
}
END{
print "Total logins : ",count;
print "Total dates : ",datecount;
print "Average logins : ",count/datecount;
}'
Sample Output 样本输出
count : 20
datecount : 12
average : 1.66667
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.