Gl.ib.ly

(glibly); Just another techie blog.

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: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:
  1. My one liner is a bit long and can be made shorter; how?
  2. Can the bar charts be made with some sed voodoo?
  3. Can you reorientate the bar chart?
  4. Can you automatically calculate the best values for i?
  5. 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.
  6. Add your own unique one liner to this blog post.
Defined tags for this entry: , , , , , , ,
Vote for articles fresher than 90 days!
Current karma: 2.90 of 5, 10 vote(s) 10454 hits
Bookmark Getting logins per day in Moodle  at del.icio.us Digg Getting logins per day in Moodle Bloglines Getting logins per day in Moodle Technorati Getting logins per day in Moodle Fark this: Getting logins per day in Moodle Bookmark Getting logins per day in Moodle  at YahooMyWeb Bookmark Getting logins per day in Moodle  at Furl.net Bookmark Getting logins per day in Moodle  at blogmarks Stumble It!

0 Trackbacks

  1. No Trackbacks

2 Comments

Display comments as (Linear | Threaded)
  1. for i in $(seq -w 1 30) ; do printf $i","`grep $i/Nov /var/log/apache2/ssl_access_log | grep login| grep POST | wc -l`",\n"; done | awk -F, '{ printf $1"\t"$2"\t"; for(i=100;i LESS_THAN $2;i+=100) printf "#"; print "" }'

    Replace LESS_THAN with < -- when I put it in there your blog ate my line.

  2. My blog is bold and your line does what mine does, but with a for loop.

Add Comment


Standard emoticons like :-) and ;-) are converted to images.

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA

You can use [geshi lang=lang_name [,ln={y|n}]][/geshi] tags to embed source code snippets.