-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
256 additions
and
3 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.DS_Store | ||
bin/ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,44 @@ | ||
lilpinger | ||
========= | ||
# lilpinger | ||
|
||
A small site pinging application with email and sms notifications written in Golang | ||
|
||
lilpinger is a small site pinging tool I wrote to notify me via SMS and email if any of the properties I manage are running slow or having connection problems. Pinging each URL happens in a separate goroutine allowing for lags and errors for each URL to be independent of each other. | ||
|
||
## Features | ||
- Pings a list of stites once per ping interval in seperate go routines | ||
- SMS and email notifications for: | ||
- connection errors | ||
- response times over lag threshold | ||
|
||
## Configuration | ||
All configuration is handled inside lilpinger.toml. The following params can be set: | ||
|
||
- **PingInterval**: How often to ping each url, in seconds | ||
- **LagThreshold**: If response time is slower than this, send alert (in seconds) | ||
- **URLsFile**: Path to a text file of URLs to ping. Each URL needs to be on a new line. File path can be a relative our absolute reference. | ||
- **Twilio**: Credentials for SMS notifications via Twilio | ||
- **SMTP**: Credentials for email account to send notifications from | ||
- **Notify**: Mobile phones and emails to notify on ping errors or slow responses. | ||
|
||
## Runing lilpinger | ||
|
||
### From Go source | ||
|
||
``` | ||
go run lilpinger | ||
``` | ||
|
||
You will see output for each url in your URLsFile. | ||
|
||
### Compiled version as a foreground process | ||
This will ouput ping data to the console | ||
|
||
``` | ||
./lilpinger | ||
``` | ||
|
||
### Compiled version as a background process | ||
This will create a lilpinger.log file with lilpinger's output | ||
|
||
``` | ||
./lilpinger > lilpinger.log & | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package config | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/BurntSushi/toml" | ||
) | ||
|
||
var Params struct { | ||
LagThreshold int | ||
PingInterval int | ||
URLsFile string | ||
Notify struct { | ||
Phones []string | ||
Emails []string | ||
} | ||
SMTP struct { | ||
Email string | ||
Password string | ||
Server string | ||
Port string | ||
} | ||
Twilio struct { | ||
AccountSid string | ||
AuthToken string | ||
Number string | ||
} | ||
} | ||
|
||
var tomlFile = "lilpinger.toml" | ||
|
||
func init() { | ||
if _, err := toml.DecodeFile(tomlFile, &Params); err != nil { | ||
log.Fatal(err) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"net/http" | ||
"strings" | ||
"time" | ||
|
||
"lilpinger/config" | ||
"lilpinger/tools" | ||
) | ||
|
||
var ( | ||
c = make(chan string) | ||
) | ||
|
||
func main() { | ||
// read our text file of urls | ||
fileData, err := ioutil.ReadFile(config.Params.URLsFile) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
urls := []string{} | ||
// split at new lines | ||
urls = strings.Split(string(fileData), "\n") | ||
|
||
for _, v := range urls { | ||
log.Println(v) | ||
if v != "" { | ||
go ping(v) | ||
} | ||
} | ||
|
||
// output logs to the terminal | ||
for i := range c { | ||
fmt.Println(i) | ||
} | ||
} | ||
|
||
func ping(url string) { | ||
// loop forever | ||
for { | ||
// lag timer start | ||
start := time.Now() | ||
|
||
// make our request | ||
res, err := http.Get(url) | ||
|
||
if err != nil { | ||
msg := "Error:" + err.Error() | ||
|
||
fmt.Println(msg) | ||
|
||
c <- msg | ||
reportError(msg) | ||
} else { | ||
lag := time.Since(start) | ||
var msg string | ||
|
||
// running slow | ||
if lag > time.Duration(config.Params.LagThreshold)*time.Second { | ||
msg = url + " lag: " + lag.String() | ||
reportError(msg) | ||
} | ||
|
||
msg = url + ", lag: " + lag.String() | ||
c <- msg | ||
} | ||
|
||
res.Body.Close() | ||
time.Sleep(time.Duration(config.Params.PingInterval) * time.Second) | ||
} | ||
} | ||
|
||
func reportError(msg string) { | ||
tools.SendSMS(msg) | ||
tools.SendMail(msg) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# If response time is slower than this, send alert (in seconds) | ||
LagThreshold = 10 | ||
|
||
# How often to ping (in seconds) | ||
PingInterval = 60 | ||
|
||
# A text file of URLs to ping seperated by new lines. | ||
# can be a relative our absolute reference | ||
URLsFile = "urls.txt" | ||
|
||
# Twilio creds for SMS notifications | ||
[Twilio] | ||
AccountSid = "" | ||
AuthToken = "" | ||
Number = "+16191234567" | ||
|
||
# SMTP email server credentials to send notifications from | ||
# uses PlainAuth and has been tested to work with GMail | ||
[SMTP] | ||
Email = "[email protected]" | ||
Password = "xxx1234" | ||
Server = "smtp.gmail.com" | ||
Port = "587" | ||
|
||
# emails and phone numbers to notify when there is a ping error | ||
# or the response is slower than the LagThreshold | ||
[Notify] | ||
Phones = [ | ||
"+16191234567" | ||
] | ||
Emails = [ | ||
"[email protected]" | ||
] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package tools | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/sfreiberg/gotwilio" | ||
|
||
"lilpinger/config" | ||
) | ||
|
||
func SendSMS(msg string) { | ||
if config.Params.Twilio.AccountSid == "" || config.Params.Twilio.AuthToken == "" { | ||
log.Println("twilio creds not set") | ||
return | ||
} | ||
|
||
twilio := gotwilio.NewTwilioClient(config.Params.Twilio.AccountSid, config.Params.Twilio.AuthToken) | ||
|
||
from := config.Params.Twilio.Number | ||
|
||
for i := 0; i < len(config.Params.Notify.Phones); i++ { | ||
twilio.SendSMS(from, config.Params.Notify.Phones[i], msg, "", "") | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package tools | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/smtp" | ||
|
||
"lilpinger/config" | ||
) | ||
|
||
func SendMail(msg string) { | ||
if config.Params.SMTP.Email == "" || config.Params.SMTP.Password == "" || config.Params.SMTP.Server == "" || config.Params.SMTP.Port == "" { | ||
log.Println("SMTP creds not set") | ||
} | ||
|
||
auth := smtp.PlainAuth( | ||
"", | ||
config.Params.SMTP.Email, | ||
config.Params.SMTP.Password, | ||
config.Params.SMTP.Server, | ||
) | ||
|
||
server := fmt.Sprintf("%v:%v", config.Params.SMTP.Server, config.Params.SMTP.Port) | ||
err := smtp.SendMail( | ||
server, | ||
auth, | ||
config.Params.SMTP.Email, | ||
config.Params.Notify.Emails, | ||
[]byte("Subject: "+msg+"\r\n\r\n"+msg), | ||
) | ||
|
||
if err != nil { | ||
fmt.Println("Error sending mail - %v - message: ", err, msg) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
http://github.com | ||
http://google.com |