Last active
August 29, 2015 14:23
-
-
Save simonszu/9f72f1064f7bb79e8f00 to your computer and use it in GitHub Desktop.
A munin plugin to monitor some statistics of a twitter account
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
#!/usr/bin/env ruby | |
# Munin plugin to track some twitter stats. | |
# Requirements: The twitter gem ('gem install twitter') | |
# Go to dev.twitter.com and register your own app. Get the oauth credentials for | |
# yourself. | |
# | |
# Paste the following to /etc/munin/plugin-conf.d/munin-node: | |
# | |
# [twitter*] | |
# command /usr/local/rvm/bin/ruby-1.9.3-p392 %c # Change this according to your path but preserve the %c | |
# env.CONSUMER_KEY <KEY_GOES_HERE> | |
# env.CONSUMER_SECRET <SECRET_GOES_HERE> | |
# env.ACCESS_TOKEN <TOKEN> | |
# env.ACCESS_TOKEN_SECRET <SECRET> | |
require 'twitter' | |
require 'nokogiri' | |
require 'open-uri' | |
$0.match(/twitter_(followers|following|statuses|favsgiven|favsgot)_(.+)/) | |
action = $1 | |
username = $2 | |
consumer_key = ENV['CONSUMER_KEY'] | |
consumer_secret = ENV['CONSUMER_SECRET'] | |
access_token = ENV['ACCESS_TOKEN'] | |
access_token_secret = ENV['ACCESS_TOKEN_SECRET'] | |
# Create the twitter client | |
client = Twitter::REST::Client.new do |config| | |
config.access_token = access_token | |
config.access_token_secret = access_token_secret | |
config.consumer_key = consumer_key | |
config.consumer_secret = consumer_secret | |
end | |
page = Nokogiri::HTML(open("http://de.favstar.fm/users/#{username}/recent")) | |
values = page.css('div.fs-user-stats div.fs-value') | |
if (ARGV[0].eql? "suggest") | |
puts "followers_<USERNAME>" | |
puts "following_<USERNAME>" | |
puts "statuses_<USERNAME>" | |
puts "favsgiven_<USERNAME>" | |
puts "favsgot_<USERNAME>" | |
end | |
if (action.eql? "followers") | |
if (ARGV[0].eql? "config") | |
puts "graph_title Followers for #{username}" | |
puts "graph_category Twitter" | |
puts "graph_vlabel Count" | |
puts "graph_info This graph shows the evolution of a follower count for a certain twitter account over the time." | |
puts "follower.label Followers" | |
exit | |
end | |
puts "follower.value #{client.user(username).followers_count}" | |
elsif (action.eql? "following") | |
if (ARGV[0].eql? "config") | |
puts "graph_title #{username} follows" | |
puts "graph_category Twitter" | |
puts "graph_vlabel Count" | |
puts "graph_info This graph shows the evolution of a friends count for a certain twitter account over the time." | |
puts "friends.label Friends" | |
exit | |
end | |
puts "friends.value #{client.user(username).friends_count}" | |
elsif (action.eql? "statuses") | |
if (ARGV[0].eql? "config") | |
puts "graph_title Tweets of #{username}" | |
puts "graph_category Twitter" | |
puts "graph_vlabel Count" | |
puts "graph_info This graph shows the evolution of the tweets count for a certain twitter account over the time." | |
puts "tweets.label Tweets" | |
exit | |
end | |
puts "tweets.value #{client.user(username).statuses_count}" | |
elsif (action.eql? "favsgiven") | |
if (ARGV[0].eql? "config") | |
puts "graph_title #{username}'s favs given" | |
puts "graph_category Twitter" | |
puts "graph_vlabel Count" | |
puts "graph_info This graph shows the evolution of a fav count of a certain twitter account over the time." | |
puts "favsgiven.label Favs given" | |
exit | |
end | |
puts "favsgiven.value #{client.user(username).favourites_count}" | |
elsif (action.eql? "favsgot") | |
if (ARGV[0].eql? "config") | |
puts "graph_title #{username}'s favs got" | |
puts "graph_category Twitter" | |
puts "graph_vlabel Count" | |
puts "graph_info This graph shows the evolution of a fav count for a certain twitter account over the time." | |
puts "favsgot.label Favs" | |
exit | |
end | |
puts "favsgot.value #{values[0].inner_text.gsub(/,/, '')}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment