5 Replies Latest reply on Mar 30, 2017 7:57 AM by phoffmann

    Monitoring the number of logins on a Mac

    csoto Specialist

      We need to record the number of times particular Mac systems are used. One way to do this is to count how many times someone logs into the system. We can take advantage of the Mac OS X "Login Hook" feature. This is a script that runs whenever someone logs into the console window (not an SSH session).  A decent article on these is found at http://www.mactech.com/articles/mactech/Vol.25/25.07/2507MacEnterprise-Scriptingopportunities2/index.html.

       

      The following script looks for the current number of logins from the Management Suite parameters list where Custom Data is stored. It increments this by one, then stores it back into that list:

       

      #!/bin/bash

       

      # ldmaclogincount - Reads /Library/Application Support/LANDesk/data/ldscan.core.data Custom Data Attribute

      # “Custom Data - Mac - Login Count” then increments it and writes it back. This will be updated

      # on the next Inventory Scan.

      #

      # SYNOPSIS

      #   ldmaclogincount

      #

      # REVERENCES

      # https://community.landesk.com/support/docs/DOC-9232

       

      # Plist variables

      plistFile="/Library/Application Support/LANDesk/data/ldscan.core.data"

       

      # Read the currently stored value into $currentcount

       

      currentcount=$(/usr/bin/defaults read "${plistFile}" "Custom Data - Mac - Login Count") || currentcount=0

       

      # Increment $currentcount by 1

       

      currentcount=$(($currentcount +1))

       

      # Write the data to the LANDesk plist

      /usr/bin/defaults write "${plistFile}" "Custom Data - Mac - Login Count" "$currentcount"

       

      # eof

       

      Upon subsequent Inventory scans, this count will be stored in Custom Data > Mac > Login Count (but feel free to change this).

       

      Be aware that the first time this is run on any Mac, this Custom Data will be considered "Unknown Items." You have to allow this attribute per Custom Data is not Entered - Using the Unknown Items Inventory Tool

       

      Charles

        • 1. Re: Monitoring the number of logins on a Mac
          csoto Specialist

          Similarly, there's a simple way to get a "total number of hours logged in" (e.g. "Usage"):

           

          /usr/bin/defaults write "/Library/Application Support/LANDesk/data/ldscan.core.data" "Custom Data - Mac - Usage" "`/usr/sbin/ac | /usr/bin/awk '{print $2}'`"
          

           

          The command "ac" without arguments gives a running total of hours the system has been logged in. Adding the above statement to the crontab or running as s Custom Script should add it on demand.

           

          Charles

          • 2. Re: Monitoring the number of logins on a Mac
            csoto Specialist

            And to get a "Last Logon Date" use:

             

            /usr/bin/defaults write "/Library/Application Support/LANDesk/data/ldscan.core.data" "Custom Data - Mac - Last Logon Date" "`/usr/bin/last | /usr/bin/grep console | /usr/bin/head -1 | /usr/bin/awk '{print $3 " " $4 " " $5 " " $6}'`"
            

             

            This will be in the format "Wed Mar 15 13:14" not the usual "datetime" format. I leave that to the reader to tweak.

             

            Charles

            • 3. Re: Monitoring the number of logins on a Mac
              csoto Specialist

              Note that to run any of these as Custom Scripts, you need to "escape" the double quotation marks by placing a \ in front of them. For example, the following runs the above two "Usage" and "Last Logon Date" bits from within a Custom Script:

               

              [MACHINES_MAC]
              REMEXEC01=/usr/bin/defaults write \"/Library/Application Support/LANDesk/data/ldscan.core.data\" \"Custom Data - Mac - Usage\" \"`/usr/sbin/ac | /usr/bin/awk '{print $2}'`\"
              REMEXEC02=/usr/bin/defaults write \"/Library/Application Support/LANDesk/data/ldscan.core.data\" \"Custom Data - Mac - Last Logon Date\" \"`/usr/bin/last | /usr/bin/grep console | /usr/bin/head -1 | /usr/bin/awk '{print $3 \" \" $4 \" \" $5 \" \" $6}'`\"
              REMEXEC03=/Library/Application\ Support/LANDesk/bin/ldiscan
              

               

              It also runs a ldiscan afterwards, so you can get the Custom Data brought in through Inventory. Don't forget to allow this unknown data per the document referenced above.

               

              Charles

              • 4. Re: Monitoring the number of logins on a Mac
                csoto Specialist

                I decided to go ahead and use the built-in "OS - Last Logon Date" attribute, to be consistent with the way it's done in Windows:

                 

                /usr/bin/defaults write "/Library/Application Support/LANDesk/data/ldscan.core.data" "OS - Last Logon Date" "`/bin/date -j -f "%b %d %H:%M" "$(/usr/bin/last | /usr/bin/grep console | /usr/bin/head -1 | /usr/bin/awk '{print $4 " " $5 " " $6}')" "+%s"`"
                

                 

                This will output something like "1490642861" (a Unix timestamp, which is apparently what the Inventory Service on the core is expecting).

                 

                For use in a Custom Script, "escape" the double quotes (there's a lot of them!):

                 

                /usr/bin/defaults write \"/Library/Application Support/LANDesk/data/ldscan.core.data\" \"OS - Last Logon Date\" \"`/bin/date -j -f \"%b %d %H:%M\" \"$(/usr/bin/last | /usr/bin/grep console | /usr/bin/head -1 | /usr/bin/awk '{print $4 \" \" $5 \" \" $6}')\" \"+%s\"`\"
                

                 

                Charles

                1 of 1 people found this helpful
                • 5. Re: Monitoring the number of logins on a Mac
                  phoffmann SupportEmployee

                  Nice of you to share this with us.

                   

                  Thanks - appreciate it .

                   

                  (And good catch on the common thing people trip over with new / unexpected custom data) .