Add separate views for the top servers on a map.
[xonotic/xonstat.git] / xonstat / util / xs_submit.go
1 package main
2
3 import (
4         "bufio"
5         "bytes"
6         "flag"
7         "fmt"
8         "io/ioutil"
9         "net/http"
10         "os"
11 )
12
13 /* xs_submit takes a file containing a single XonStat request and submits it
14    to the server URL specified */
15 func main() {
16         fn := flag.String("file", "xonstat.log", "Logfile from XonStat")
17         url := flag.String("url", "http://localhost:6543/stats/submit", "XonStat submission URL")
18         out := flag.Bool("out", false, "logs the response body to <file>.out")
19         flag.Parse()
20
21         body, err := ioutil.ReadFile(*fn)
22         if err != nil {
23                 fmt.Printf("Issue opening file %s\n", *fn)
24                 os.Exit(1)
25         }
26         contentlength := int64(len(body))
27
28         r := bytes.NewReader(body)
29
30         req, _ := http.NewRequest("POST", *url, r)
31         req.ContentLength = contentlength
32         res, _ := http.DefaultClient.Do(req)
33         defer res.Body.Close()
34
35         fmt.Printf("%s: %s\n", *fn, res.Status)
36
37         if *out {
38                 // open the output file for the response
39                 of, err := os.Create(*fn + ".out")
40                 if err != nil {
41                         fmt.Printf("Issue creating file %s.out\n", *fn)
42                         os.Exit(1)
43                 }
44                 defer of.Close()
45
46                 bo := bufio.NewWriter(of)
47                 defer bo.Flush()
48
49                 scanner := bufio.NewScanner(res.Body)
50                 for scanner.Scan() {
51                         fmt.Fprintln(bo, scanner.Text())
52                 }
53         }
54 }