Recently I was new to the world of Twitter. Once I started I found it very addictive both for the information you could find on there and the possibilities of which it could be used.
The following script should be run via cron, if you run it on anything else other than a Mac you will need to replace the Mac specific HardwareMonitor application with one for your operating system. The code started to add functionality to allow tweeting DM's to the computer and it would reply with certain information like IP address. However, since I found TweetMyMac I have abandoned this functionality. I could be persuaded to re-visit this script an the DM aspect if there was a reason / idea.
The crontab code I use for this on my Mac is:
15 * * * * /Users/cranie/mytwitter/twitter
basically (if you are unfamiliar with cron) run crontab -e and enter the above (replacing the /Users... part with the location to your script). This will then run every 15minutes and tweet the appropriate messages if required.
Updates to come:
- Random messages (i.e. damn its 55deg in here or Oh my I am on fire.... etc - ideas welcome)
- DM if there is any useful application for implementing this
- ???
The script:
#--- START ---
#!/bin/sh
# Limit thresholds before script alerts to twitter
CPU_LIM=55
PROC_LIM=100
DISK_LIM=95
USERNAME="your twitter username"
PASSWORD="your twitter password"
URL=http://twitter.com/statuses/update.json
CPU=$(/Applications/HardwareMonitor.app/Contents/MacOS/hwmonitor 2> /dev/null | grep "SMC CPU A DIODE" | sed -e 's/.*: //g' -e 's/ C//g')
if [ ${CPU} -ge ${CPU_LIM} ]
then
MESSAGE="${MESSAGE}Its roasting in here: CPU Temperature is ${CPU} deg C. "
fi
DISK=$( df -m | grep "/dev/disk0s2" | awk ' { print $5 } ' | sed 's/%//g' )
if [ ${DISK} -ge ${DISK_LIM} ]
then
MESSAGE="${MESSAGE} I'm getting full! Root disk is ${DISK}% full. "
fi
PROC=$( ps -ef | wc -l | awk ' { print $1 } ')
if [ ${PROC} -ge ${PROC_LIM} ]
then
MESSAGE="${MESSAGE}${PROC} processes running. "
fi
UPTIME=$( uptime | awk ' { print $3 } ' | sed 's/:.*//g' )
TIMER=$( uptime | awk ' { print $5 } ' | sed -e 's/,//g' -e 's/:.*//g' )
UPTIME_UNIT=$( uptime | awk ' { print $4 } ' | sed 's/,//g' )
if [ "$(( ${UPTIME} % 8 ))" -eq 0 -a "${UPTIME_UNIT}" == "days" -a "${TIMER}" -eq 1 ]
then
MESSAGE="${MESSAGE}Up ${UPTIME} ${UPTIME_UNIT}. "
fi
if [ "${MESSAGE}" != "" ]
then
#echo "Posting: ${MESSAGE}"
curl --basic --user "${USERNAME}:${PASSWORD}" --data-ascii "status=`echo ${MESSAGE}|tr ' ' '+'`" "${URL}" -o /dev/null
else
echo "Nothing to post"
fi
# --- Checks for ask for info below here:
URL2=https://twitter.com/statuses/mentions.xml
#TIME_CHK=`date | awk ' { split($4,A,":") ; if ( A[2] <= 14 ) { printf("%02d:%02d:%02d\n", A[1] - 1, A[2] -15, A[3]) } else { printf("%02d:%02d:%02d\n", A[1], A[2] - 15, A[3]) } } '`
# MAC
TIME_CHK=`date | awk ' { split($5,A,":") ; if ( A[2] <= 14 ) { printf("%02d:%02d:%02d\n", A[1] - 1, A[2] -15, A[3]) } else { printf("%02d:%02d:%02d\n", A[1], A[2] - 15, A[3]) } } '`
DATE_CHK="`date +'%a %h %d'`"
RUN=`curl --basic --user "${USERNAME}:${PASSWORD}" -k ${URL2} 2> /dev/null | egrep "|" |
awk ' {
if ( match($1,"created_at") != 0 ) {
printf $0
} else {
print $0
}
} ' |
grep "" |
sed -e 's/.*//' -e 's/<\/created_at>//' -e 's///' -e 's/<\/text>//' |
awk -v q=\' -v TIME_CHK=${TIME_CHK} -v DATE_CHK="${DATE_CHK}" ' {
MSG_DATE = $1 " " $2 " " $3
if ( DATE_CHK == MSG_DATE ) {
split(TIME_CHK,T,":")
split($4,M,":")
# +1 hour due to twitter being none BST
HOUR = M[1] + 1
if ( HOUR >= T[1] && M[2] >= T[2] ) {
if ( $8 == "show" ) {
if ( $9 == "time" ) {
EXTRA = EXTRA "My clock tells me its @DATE. "
}
if ( $9 == "temp" ) {
EXTRA = EXTRA "My temp is currently @TEMP. "
}
if ( $9 == "ip" ) {
EXTRA = EXTRA "Sent direct message of my IP address @IP. "
DM = DM "My IP address is @IP. "
}
}
}
}
} ' `
DATE="`date`"
RUNNER="`echo ${RUN} | sed "s/@DATE/${DATE}/g"`"
RUNNER="`echo ${RUN} | sed "s/@TEMP/${CPU}/g"`"
IP=`curl http://checkip.dyndns.org/ | awk ' { print $6 } ' | sed 's/<.*//g'`
DMER="`echo ${DM} | sed "s/@IP/${IP}/g"`"
if [ "${RUNNER}" != "" ]
then
curl --basic --user "${USERNAME}:${PASSWORD}" --data-ascii "status=`echo ${RUNNER}|tr ' ' '+'`" "${URL}" -o /dev/null
else
echo "Nothing to post"
fi
USRL3=http://twitter.com/direct_messages/new.xml
if [ "${DMER}" != "" ]
then
curl --basic --user "${USERNAME}:${PASSWORD}" -d "text=${DMER}&user=cranies" ${URL3} -o /dev/null
else
echo "Nothing to post"
fi
#--- END ---
http://pastie.org/615819