Getting logins per day in Moodle
Posted by Tariq • Tuesday, December 2. 2008 • Category: Moodle, One liners
Today I got the urge to see how many logins we had on some of our Apache servers in November. We host more than one moodle site on one server and so I wanted the total number of logins we handled. As usual this ended up becoming an awkward but useful one liner.
Lets get straight into the action:
Lets break all this down:
I like redirecting to csv file which I can open in a spreadsheet program to produce pretty charts. An example from Office is shown below.

You can also do things like showing the top 5 login days with:
This is a slightly edited version which produces some command line bar charts:
This produces something like the following.
The code to produce the graph is
I have left out a few things:
Lets get straight into the action:
sudo seq -w 1 30 | awk '{ printf $1","; system("grep " $1 "/Nov/2008 /var/log/apache2/ssl_access_log | grep login | grep POST | wc -l | tr \"\\n\" ,"); print "" }'. This code produces CSV output in the format DAY,LOGINS,.Lets break all this down:
- /var/log/apache2/ssl_access_log - This is my SSL access log (it is actually a merge of my November files which have been logrotated
- seq -w 1 30 - This gives us values 01 to 30. The -w bit gives us the leading zeros do man seq for more details. Should be seq -w 1 31 as having 0 values for extra days isn't that bad
- grep $i/Nov - Gets our dates with the i variable which changes from 01 to 30 throughout the loop
- grep login - Finds lines with login; you could be a bit more exact here!
- grep POST - Finds all the requests posted.
- wc -l - Count lines in output.
I like redirecting to csv file which I can open in a spreadsheet program to produce pretty charts. An example from Office is shown below.

You can also do things like showing the top 5 login days with:
sudo seq -w 1 30 | awk '{ printf $1","; system("grep " $1 "/Nov/2008 /va
r/log/apache2/ssl_access_log | grep login | grep POST | wc -l | tr \"\\n\" ,"); print "" }' | sort -t, -rnk2 | head -n5. Here sort -t, -rnk2 tells sort to use ',' as the field delimiter, to reverse the sort, to use a numeric key and to use the second field as the sort key. The head -n5 chops off the first 5 lines of input and displays it.This is a slightly edited version which produces some command line bar charts:
sudo seq -w 1 30 | awk '{ printf $1","; system("grep " $1 "/Nov/2008 /var/log/apache2/ssl_access_log | grep login | grep POST | wc -l | tr \"\\n\" ,"); print "" }' | awk -F, '{ printf $1"\t"$2"\t"; for(i=0;i<$2;i+=100) printf "#"; print""}'This produces something like the following.
01 1204 ############
02 1084 ##########
03 2614 ##########################
04 3068 ##############################
05 3254 ################################
06 3348 #################################
07 2806 ############################
08 1692 ################
09 1588 ###############
10 3364 #################################
11 3240 ################################
12 3106 ###############################
13 3068 ##############################
14 3178 ###############################
15 2404 ########################
16 2414 ########################
17 3496 ##################################
18 3482 ##################################
19 3336 #################################
20 3164 ###############################
21 3058 ##############################
22 2152 #####################
23 1760 #################
24 3056 ##############################
25 2908 #############################
26 2906 #############################
27 2508 #########################
28 2342 #######################
29 1650 ################
30 1448 ##############
The code to produce the graph is
awk -F, '{ printf $1"\t"$2"\t"; for(i=100;i<$2;i+=100) printf "#"; print""}'. Here awk is told to recognise ',' as the field separator. In the awk program the first two fields are printed out with tab separators. We use a loop to print out each '#' you may want to adjust the magic number 100 so that the chart prints nicely on your screen. I have left out a few things:
- My one liner is a bit long and can be made shorter; how?
- Can the bar charts be made with some sed voodoo?
- Can you reorientate the bar chart?
- Can you automatically calculate the best values for i?
- Imagine you have 5 hosted sites (each site's URL starts /a/, /b/, /c/, /d/, /e/) rewrite the one liner to add an extra country field to show logins for each country.
- Add your own unique one liner to this blog post.
2 Comments
Replace LESS_THAN with < -- when I put it in there your blog ate my line.
Add Comment