Advertisement
Guest User

Untitled

a guest
Sep 30th, 2014
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "encoding/json"
  5. "fmt"
  6. "github.com/bradfitz/gomemcache/memcache"
  7. "os"
  8. "time"
  9. )
  10.  
  11. // Configuration encapsulates the result of reading the JSON configuration
  12. // file.
  13. type Configuration struct {
  14. URLs []string
  15. Memcached string
  16. }
  17.  
  18. // loadConfig loads a configuration file in JSON format and returns a
  19. // Configuration instance.
  20. func loadConfig(path string) (Configuration, error) {
  21. file, _ := os.Open(path)
  22. defer file.Close()
  23.  
  24. decoder := json.NewDecoder(file)
  25. configuration := Configuration{}
  26. err := decoder.Decode(&configuration)
  27.  
  28. return configuration, err
  29. }
  30.  
  31. func main() {
  32. type Result struct {
  33. url string
  34. status Status
  35. }
  36.  
  37. rc := make(chan Result)
  38.  
  39. configuration, err := loadConfig("config.json")
  40.  
  41. if err != nil {
  42. fmt.Println("Error :", err)
  43. return
  44. }
  45.  
  46. sites := make(map[string]*Site, len(configuration.URLs))
  47.  
  48. for _, url := range configuration.URLs {
  49. sites[url] = &Site{url, UNCHECKED}
  50. }
  51.  
  52. mc := memcache.New(configuration.Memcached)
  53.  
  54. sites_output := make(map[string]bool)
  55.  
  56. for {
  57. for _, site := range sites {
  58. go func(site *Site, rc chan Result) {
  59. status, _ := site.Status()
  60. rc <- Result{site.url, status}
  61. }(site, rc)
  62. }
  63.  
  64. for i := 0; i < len(sites); i++ {
  65. res := <-rc
  66. site := sites[res.url]
  67. if site.last_status != res.status {
  68. sites[res.url].last_status = res.status
  69. }
  70. }
  71.  
  72. for k, v := range sites {
  73. sites_output[k] = v.last_status == 2
  74. }
  75.  
  76. site_json, err := json.Marshal(sites_output)
  77.  
  78. if err != nil {
  79. panic(err)
  80. }
  81. mc.Set(&memcache.Item{
  82. Key: "mission_control.sites",
  83. Value: site_json,
  84. })
  85. time.Sleep(time.Second * 5)
  86. }
  87. }
  88.  
  89. package main
  90.  
  91. import (
  92. "net/http"
  93. )
  94.  
  95. type Status int
  96.  
  97. const (
  98. UNCHECKED Status = iota
  99. DOWN
  100. UP
  101. )
  102.  
  103. // The Site struct encapsulates the details about the site being monitored.
  104. type Site struct {
  105. url string
  106. last_status Status
  107. }
  108.  
  109. // Site.Status makes a GET request to a given URL and checks whether or not the
  110. // resulting status code is 200.
  111. func (s Site) Status() (Status, error) {
  112. resp, err := http.Get(s.url)
  113. status := s.last_status
  114.  
  115. if (err == nil) && (resp.StatusCode == 200) {
  116. status = UP
  117. } else {
  118. status = DOWN
  119. }
  120.  
  121. return status, err
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement