Skip to content

Commit 70b4cc5

Browse files
authored
Merge pull request #2 from chrisjbowen/master
Added site.go to support website monitoring, updated readme
2 parents a5bcb78 + 0dd6052 commit 70b4cc5

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ Flags:
2222
-d, --debug Print debug logging
2323
-h, --help help for spinner
2424
-t, --tail string Path to file to tail and pipe to STDOUT.
25+
26+
Service Usage:
27+
spinner service [name] [flags]
28+
29+
Site Usage:
30+
spinner site [url] [flags]
2531
```
2632

2733

cmd/site.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Copyright © 2017 Ticketmaster
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package cmd
15+
16+
import (
17+
"fmt"
18+
"log"
19+
"net/http"
20+
"os"
21+
s "strings"
22+
"time"
23+
24+
"github.com/spf13/cobra"
25+
)
26+
27+
var urlFlag string
28+
29+
func queryPage(u string) {
30+
31+
var fullURL string
32+
33+
if s.HasPrefix(u, "http://") || s.HasPrefix(u, "https://") {
34+
fullURL = u
35+
} else {
36+
fullURL = "http://" + u
37+
}
38+
39+
fmt.Println("Full URL being monitored:", fullURL)
40+
for {
41+
42+
resp, err := http.Get(fullURL)
43+
44+
if err != nil {
45+
log.Fatal("An error occurred during the request:", err)
46+
}
47+
48+
if resp.StatusCode != 200 {
49+
log.Fatal("Status code != 200")
50+
} else if debugFlag {
51+
log.Println("Status Code:", resp.StatusCode)
52+
}
53+
54+
resp.Body.Close()
55+
56+
time.Sleep(1000 * time.Millisecond)
57+
}
58+
}
59+
60+
// siteCmd represents the site command
61+
var siteCmd = &cobra.Command{
62+
Use: "site [url]",
63+
Short: "Watch a Site",
64+
Aliases: []string{"url", "address"},
65+
Example: "spinner.exe site http://localhost -t c:\\iislog\\W3SVC\\u_extend1.log",
66+
Long: `Poll Web Site by Get request and terminate this process if
67+
the a >300 status code is returned.
68+
69+
Use this as the entrypoint for a container to stop the container if
70+
the given service stops.`,
71+
Run: func(cmd *cobra.Command, args []string) {
72+
if len(args) < 1 {
73+
fmt.Println("site needs a url for the command")
74+
os.Exit(1)
75+
}
76+
if debugFlag {
77+
fmt.Println("with debug")
78+
}
79+
if tailFile != "" {
80+
go TailLog()
81+
}
82+
83+
queryPage(args[0])
84+
85+
},
86+
}
87+
88+
func init() {
89+
RootCmd.AddCommand(siteCmd)
90+
91+
//siteCmd.Flags().StringVarP(&urlFlag, "url", "u", "http://localhost/", "Url to watch")
92+
93+
}

0 commit comments

Comments
 (0)