{"id":56,"date":"2010-05-06T18:25:07","date_gmt":"2010-05-06T22:25:07","guid":{"rendered":""},"modified":"2011-02-01T23:54:16","modified_gmt":"2011-02-02T04:54:16","slug":"user-time-logged-in","status":"publish","type":"post","link":"https:\/\/pappp.net\/?p=56","title":{"rendered":"User Time Logged In"},"content":{"rendered":"\n<p>A lab I sometimes do computer work for asked for a mechanism to account how long each user spends logged in on some *nix (Linux and ancient Solaris) boxes.  I don&#8217;t know of a &#8220;proper&#8221; solution, so I did things the UNIX way: I wrote a stupid little awk script that parses the output of last -R to get a minute total.  This isn&#8217;t a terribly clean or safe script, but it does &#8220;just enough&#8221; input sanitizing, and seems to work.  Some (Linux) boxes I have access to appear to clear\/archive\/rotate wtmp on boot, so it doesn&#8217;t help on those.<br \/>\nDoes anyone know of a better way to do this? Is there some utility I&#8217;m forgetting? Is this script dangerous in some way I&#8217;m not thinking of? Script follows:<\/p>\n<pre>#!\/bin\/sh\nlast&nbsp;-R&nbsp;|&nbsp;\nawk&nbsp;'\n{\n\tuser=$1;\n\tuselength=$9;\n\t\n#\tCheck&nbsp;line&nbsp;format\n\tif&nbsp;(NF&nbsp;!=&nbsp;9)&nbsp;{\n\tprint&nbsp;\"Line&nbsp;\"&nbsp;NR&nbsp;\"&nbsp;discarded,&nbsp;\"&nbsp;NF&nbsp;\"&nbsp;entries.&nbsp;(Reboot&nbsp;messages,&nbsp;etc.)\"\n\t}\n\telse&nbsp;if(&nbsp;$NF&nbsp;==&nbsp;\"in\"){\n\tprint&nbsp;\"Line&nbsp;\"&nbsp;NR&nbsp;\"&nbsp;discarded,&nbsp;still&nbsp;logged&nbsp;in.\"\n\t}\n\telse{\n\t\n\tlogins[user]++;\n#&nbsp;These&nbsp;times&nbsp;are&nbsp;(Days+Hours:Minutes)\n\tparsedtime=0;\n\tgsub(\"[()]\",\"\",uselength);\n\tsplit(uselength,&nbsp;sptime,&nbsp;\"[+:]\");\n\tparsedtime+=sptime[1]*24*60;\n\tparsedtime+=sptime[2]*60;\n\tparsedtime+=sptime[3];\n\tlogintime[user]+=parsedtime;\n\t}\n}\n\t\nEND&nbsp;{\n\tfor&nbsp;(i&nbsp;in&nbsp;logins)&nbsp;{\n\t\tprint&nbsp;i&nbsp;\"&nbsp;Logged&nbsp;in&nbsp;\"&nbsp;logins[i]&nbsp;\"&nbsp;times,&nbsp;for&nbsp;a&nbsp;total&nbsp;of&nbsp;\"&nbsp;logintime[i]&nbsp;\"&nbsp;minutes\";\n\t}\n}'\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A lab I sometimes do computer work for asked for a mechanism to account how long each user spends logged in on some *nix (Linux and ancient Solaris) boxes. I don&#8217;t know of a &#8220;proper&#8221; solution, so I did things &hellip; <a href=\"https:\/\/pappp.net\/?p=56\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,8,1,12],"tags":[],"class_list":["post-56","post","type-post","status-publish","format-standard","hentry","category-computers","category-diy","category-general","category-oldblog"],"_links":{"self":[{"href":"https:\/\/pappp.net\/index.php?rest_route=\/wp\/v2\/posts\/56","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pappp.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pappp.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pappp.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pappp.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=56"}],"version-history":[{"count":0,"href":"https:\/\/pappp.net\/index.php?rest_route=\/wp\/v2\/posts\/56\/revisions"}],"wp:attachment":[{"href":"https:\/\/pappp.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=56"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pappp.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=56"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pappp.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=56"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}