Skip to content

Commit

Permalink
Finish email notifier, start converting readme to textiile
Browse files Browse the repository at this point in the history
  • Loading branch information
Brenton Fletcher authored and Brenton Fletcher committed May 13, 2009
1 parent 0d02c31 commit d992daa
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 9 deletions.
79 changes: 79 additions & 0 deletions README.textile
@@ -0,0 +1,79 @@
h1. Webstats

Webstats is a server and clients that monitors your servers performance (CPU usage, memory usage, disk usage, disk activity, url load time) and allows you to get notifications on your computer when the server is having problems, as well as showing (http://kimag.es/share/91090734.png) the current performance stats for the server on a web page.

Webstats has 2 components: the _server_ and one or more _clients_.

h2. Server

The server runs on the computer whose performance you want to monitor; the server application's job is to (1) monitor the computer's performance and (2) report these performance statistics through a webserver that the server application runs.

See Installation below for install instructions.

The server application executes in the background and starts a tiny webserver on port 9970. You can view the performance stats for your server by going to http://<server's hostname>:9970/, for example if my server was under the domain name bloople.net, I would go to http://bloople.net:9970/. The statistics on this page automatically update every 5 seconds. You may need to open port 9970 if you have a firewall on your server. Note that the various clients get their data via the webserver the server application runs, just like the built-in stats page.

The statistic provided by the server application are:

* CPU usage and load average
* Memory (RAM) usage, with free, free - buffers, and total counts
* Disk usage by mount point
* Disk activity for reads and writes
* URL monitoring, to load URL's you specify to ensure they work

The server application will warn you of any of these statistics indicate a problem on the server - for example if you've have < 5MB free RAM for the last 12 seconds, that indicates a potential problem on your server that needs looking at. If you're looking at the performance statistics web page, the warnings will be presented by higlighting the perf stat that's causing the warning; when you're using a client application, it will present warnings to you as it sees fit.

You can configure at what thresholds the warnings get generated, as well as the URL's to monitor, by editing ~/.webstats. This file is generated the first time you run webstats, so it's pre-filled with sensible defaults; but you can change them as you see fit.

You can prevent public access to your webstats by invoking the server application with a single argument (e.g. ruby webstats.rb <password>). Then, when you visit the webstats, you'll be prompted for a username and password; the username is always 'webstats', and the password is the password specified just above.

The server application requires Linux kernel 2.6+, will not work in *BSD or OS X; the server application requires only Ruby; it does not rely on rubygems or any external libraries that aren't included with Ruby; the server application uses very little RAM, approximately 10MB.

All the client applications depend on the server application being run to work.

h2. Clients

If all you want to do is be able to view the performance statistics for your server in a web browser, then you don't need to use any of these clients; going to http://<server's hostname>:9970/ will do just fine. But if you want to have Growl or email notification when something goes wrong, without having to look at a web page all the time, then you'll want one of the client applications that come with Webstats.

Right now there's only one client application available, which is a Growl notifier. The Growl notifier only works on OS X, with RubyCocoa installed. This application runs in the background and monitors the server; if the server goes under high CPU load for more than a few seconds, or if the server is nearly out of memory, you'll get a Growl notification saying what the problem is. This way, you can just set and forget, knowing that Webstats will report any problems.

To run the Growl notifier, first install Webstats on your local machine, using the installation instructions below (Skip the step that starts the server application). Then, if you installed webstats locally via rubygems, run:
~$ webstats_growl_notifier

Otherwise, if you installed webstats manually, run:
~$ cd webstats/clients/growl_notifier
~/webstats/clients/growl_notifier$ ruby growl_notifier.rb

The first time you run the growl notifier, it will create a configuration file at ~/.webstats_clients; you must edit this file to set the URL's for the growl notifier to monitor. The URL's should the the hostnames of the servers you want to monitor, along with the correct port number (e.g. http://bloople.net:9970/).

Once you've edited the configuration file to add your URL's, run the notifier again; the Growl notifier will then run in the background and notify you od any warnings or danger situations on the server.

h2. Installation

You can now install webstats using rubygems; this saves you a bit of hassle. To install webstats on the server via rubygems, ensure github gems are in your gems sources; then run:
~$ sudo gem install bloopletech-webstats

To run the server application, run:
~$ webstats

The server application will start in the background.

If you don't want to use rubygems, then follow the below instructions:
To install the server app, ssh into the computer you want to monitor. then run:
~$ git clone git://github.com/bloopletech/webstats.git
~$ cd webstats
~/webstats$ cd server/data_providers
~/webstats/server/data_providers$ ruby extconf.rb
~/webstats/server/data_providers$ make

To run the server app, ssh into the computer you want to monitor. then run:
~$ cd webstats
~/webstats$ ruby server/webstats.rb

h2. Todo

* Add email notifier to client applications.
* More client applications.
* Extend server to work on *BSD (including OS X).
* Make webstats server optionally install itself to run at boot.
* Make client applications optionally install themselves at boot, where suitable.
* (Maybe) Move all server code into it's own module and make code work without running the web server, so you can use it to just grab stats in your own code.
17 changes: 13 additions & 4 deletions clients/email_notifier/email_notifier.rb
Expand Up @@ -11,10 +11,19 @@ def initialize(settings = {}, read_config = true)

private
def send_mail(subject, message)
@settings[:recipent]
system("sendmail -oi", "-f", @settings[:recipent])
puts subject
puts message
sm = @settings[:mail_server]

msg = <<END_OF_MESSAGE
From: Webstats Email Notifier <#{@settings[:recipient]}>
To: #{@settings[:recipient]} <#{@settings[:recipient]}>
Subject: #{subject}
Date: #{Time.now.rfc2822}
Message-Id: <#{Time.now.to_i}.#{rand(10000000)}@#{sm[:domain]}>
#{message}
END_OF_MESSAGE

Net::SMTP.start(sm[:address], sm[:port], sm[:domain], sm[:username], sm[:password], sm[:authentication]) { |smtp| smtp.send_message msg, @settings[:recipient], @settings[:recipient] }
end

def failed_url(url, password, exception)
Expand Down
5 changes: 3 additions & 2 deletions clients/simple_notifier.rb
@@ -1,6 +1,9 @@
require 'yaml'
require 'net/http'
require 'uri'
require 'thread'
require 'time'
require 'net/smtp'

def daemonize!
return if $DAEMONIZE == false
Expand Down Expand Up @@ -64,7 +67,6 @@ def start
@settings[:urls].each do |url|
threads << Thread.new do
url[:mutex].synchronize do
puts "requesting for #{url[:url]}"
url[:data] = make_request(URI.join(url[:url], "update"), url[:password])
url[:bad] = url[:data].sort { |a, b| b[1]['importance'].to_f <=> a[1]['importance'].to_f }.select { |(k, v)| !v['status'].nil? && v['status'] != '' }
url[:last_warnings] = url[:warnings] || []
Expand All @@ -76,7 +78,6 @@ def start
url[:changed] = (!url[:warnings].empty? || !url[:dangers].empty?) && (!url.key?(:changed) or (url[:warnings].length > url[:last_warnings].length) or (url[:dangers].length > url[:last_dangers].length))
url[:time_past] = url[:last_time] != 0 && (Time.now - url[:last_time]) > 60
url[:last_time] = Time.now if url[:changed] || url[:last_time].nil? || url[:time_past]
puts "done for #{url[:url]}"
end
end
end
Expand Down
5 changes: 2 additions & 3 deletions server/webstats.rb
@@ -1,3 +1,4 @@
#NO GEM DEPENDENCIES FTW
require 'webrick'
require 'yaml'

Expand All @@ -9,8 +10,6 @@
Process.detach(pid)
exit
end
#$stdout = File.new('/dev/null', 'w')
#$stderr = File.new('/dev/null', 'w')
end

Thread.new do
Expand Down Expand Up @@ -140,7 +139,7 @@ def self.setup(settings)
if File.exists?(WEBSTATS_PATH)
$settings = YAML.load(IO.read(WEBSTATS_PATH)).symbolize_keys!
else
$settings['webstats'] = { 'password' => nil, :clients => [] }
$settings['webstats'] = { 'password' => nil, 'clients' => [] }
DataProviders::DATA_SOURCES_CLASSES.each_pair { |k, v| $settings[k.to_s] = v.default_settings.stringify_keys! }
File.open(WEBSTATS_PATH, "w") { |f| YAML.dump($settings, f) }
end
Expand Down

0 comments on commit d992daa

Please sign in to comment.