Created
June 3, 2012 03:37
-
-
Save jacaetevha/2861733 to your computer and use it in GitHub Desktop.
Mac OS/X login/logout hooks to save login/logout information to a Mongo DB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
SOURCE="${BASH_SOURCE[0]}" | |
while [ -h "$SOURCE" ] ; do SOURCE="$(readlink "$SOURCE")"; done | |
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" | |
MONGO="${MONGO:-/usr/local/bin/mongo}" | |
MONGO_USER='<mongo username>' | |
MONGO_PWD='<mongo password>' | |
MONGO_URL="<database server>:<port>/<database name>" | |
USER=$1 | |
HOST=`hostname` | |
function write_file() { | |
# | |
# creates a UUID for the login event, which will | |
# later be used by the logout script to update the | |
# appropriate record in Mongo | |
# | |
uuidgen | tr -d '\n' | cat > /Users/$USER/.uuid | |
} | |
if [[ ! -e /Users/$USER/.uuid ]] | |
then | |
# | |
# normal case: UUID file should not exist | |
# | |
write_file | |
else | |
# | |
# abnormal case: this user's login was triggered at | |
# some point but the logout event was | |
# not triggered, so trigger it now and | |
# start a new record for this login. | |
# | |
$DIR/logout-hook $USER "no previous logout event" | |
write_file | |
fi | |
EVENT_ID=`cat /Users/$USER/.uuid` | |
# | |
# make sure we have access to the internet, in case | |
# our Mongo server is running on the web somewhere | |
# (e.g. mongohq.com) | |
# | |
ping -c 1 -t 20 example.com | |
# | |
# insert the record into Mongo for this login event | |
# | |
MONGO_CMD="db.logs.insert( { name: '$USER', computer: '$HOST', event_id: '$EVENT_ID', login: new Date(), logout: null } )" | |
MONGO_CMD="$MONGO_CMD; printjson(db.runCommand( 'getlasterror' ));" | |
$MONGO $MONGO_URL -u $MONGO_USER -p $MONGO_PWD --eval "$MONGO_CMD" | |
# | |
# log the event to Mac's syslog as well, in case we | |
# need to do some post-mortem debugging | |
# | |
/usr/bin/logger -i -t login-hook "$1 logged in: $(date)" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
MONGO="${MONGO:-/usr/local/bin/mongo}" | |
MONGO_USER='<mongo username>' | |
MONGO_PWD='<mongo password>' | |
MONGO_URL="<database server>:<port>/<database name>" | |
USER=$1 | |
HOST=`hostname` | |
if [[ ! -e /Users/$USER/.uuid ]] | |
then | |
# | |
# just in case we don't have a pre-recorded UUID | |
# create one now -- more error handling would be nice | |
# | |
uuidgen | tr -d '\n' | cat > /Users/$USER/.uuid | |
fi | |
# | |
# capture the EVENT_ID and then remove the UUID file | |
# | |
EVENT_ID=`cat /Users/$USER/.uuid` | |
rm -f /Users/$USER/.uuid | |
# | |
# make sure we have access to the internet, in case | |
# our Mongo server is running on the web somewhere | |
# (e.g. mongohq.com) | |
# | |
ping -c 1 -t 20 example.com | |
# | |
# find the record in Mongo and update it for this logout event | |
# | |
MONGO_CMD="var record = db.logs.findOne( { name: '$USER', computer: '$HOST', event_id: '$EVENT_ID' } );" | |
MONGO_CMD="$MONGO_CMD record.logout = new Date(); record.duration = (record.logout - record.login)/1000.0;" | |
if [[ -z $2 ]]; then | |
MONGO_CMD="$MONGO_CMD record.reason = 'normal';" | |
else | |
MONGO_CMD="$MONGO_CMD record.reason = '$2';" | |
fi | |
MONGO_CMD="$MONGO_CMD db.logs.save(record); printjson(db.runCommand( 'getlasterror' ));" | |
$MONGO $MONGO_URL -u $MONGO_USER -p $MONGO_PWD --eval "$MONGO_CMD" | |
# | |
# log the event to Mac's syslog as well, in case we | |
# need to do some post-mortem debugging | |
# | |
/usr/bin/logger -i -t logout-hook "$USER:$EVENT_ID logged out: $(date)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment