Commit fc876f41 authored by zdc's avatar zdc

flag check fix, extend config, change html template

* added check for "-data" flag exist, without it lookis can't find html template.
* remove hardcoded router list from html template, now lookis get it from config file
* added possibility to enable/disable routers in config
parent 225bf5dc
...@@ -3,7 +3,9 @@ address = "" ...@@ -3,7 +3,9 @@ address = ""
port = "40080" port = "40080"
[router01] [router01]
enabled = "yes"
type = "JunOS" type = "JunOS"
name = "RouterAlpha"
address = "127.0.0.1" address = "127.0.0.1"
port = "22" port = "22"
user = "lookis" user = "lookis"
...@@ -13,7 +15,9 @@ traceroute_command = "traceroute as-number-lookup no-resolve" ...@@ -13,7 +15,9 @@ traceroute_command = "traceroute as-number-lookup no-resolve"
showroute_command = "show route all" showroute_command = "show route all"
[router02] [router02]
enabled = "yes"
type = "VyOS" type = "VyOS"
name = "RouterBeta"
address = "127.0.0.2" address = "127.0.0.2"
port = "22" port = "22"
user = "lookis" user = "lookis"
...@@ -24,7 +28,9 @@ traceroute_command = "traceroute" ...@@ -24,7 +28,9 @@ traceroute_command = "traceroute"
showroute_command = "/opt/vyatta/bin/vtyshow.pl show ip route" showroute_command = "/opt/vyatta/bin/vtyshow.pl show ip route"
[router03] [router03]
enabled = "no"
type = "RouterOS" type = "RouterOS"
name = "RouterGamma"
address = "127.0.0.3" address = "127.0.0.3"
port = "22" port = "22"
user = "lookis" user = "lookis"
......
...@@ -7,28 +7,37 @@ import ( ...@@ -7,28 +7,37 @@ import (
"log" "log"
"net" "net"
"net/http" "net/http"
"regexp"
"sort"
"github.com/spf13/viper" "github.com/spf13/viper"
"golang.org/x/crypto/ssh" "golang.org/x/crypto/ssh"
) )
// where is config and template // setting global variables
var datapath string var datapath string
var RouterList []Routers
// declare type for routers list
type Routers struct {
RouterId, RouterName string
}
// declare variables for page template // declare variables for page template
type DataOut struct { type DataOut struct {
RoutersList []Routers
OutputHeader, OutputBody string OutputHeader, OutputBody string
} }
// fill error details // fill error details
func PrepareError (HeaderRaw, BodyRaw string) DataOut { func PrepareError (RoutersRaw []Routers, HeaderRaw, BodyRaw string) DataOut {
log.Printf("Error! %s: \"%s\"", HeaderRaw, BodyRaw) log.Printf("Error! %s: \"%s\"", HeaderRaw, BodyRaw)
output := DataOut{ OutputHeader: HeaderRaw, OutputBody: BodyRaw } output := DataOut{ RoutersList: RoutersRaw, OutputHeader: HeaderRaw, OutputBody: BodyRaw }
return output return output
} }
// fill normal output // fill normal output
func PrepareOutput (HeaderRaw, BodyRaw string) DataOut { func PrepareOutput (RoutersRaw []Routers, HeaderRaw, BodyRaw string) DataOut {
output := DataOut{ OutputHeader: HeaderRaw, OutputBody: BodyRaw } output := DataOut{ RoutersList: RoutersRaw, OutputHeader: HeaderRaw, OutputBody: BodyRaw }
return output return output
} }
...@@ -85,7 +94,7 @@ func SSHRun (router_ip, port, username, password, command, argument, suffix stri ...@@ -85,7 +94,7 @@ func SSHRun (router_ip, port, username, password, command, argument, suffix stri
// connect to router // connect to router
connection, err := ssh.Dial("tcp", router_ip+":"+port, sshConfig) connection, err := ssh.Dial("tcp", router_ip+":"+port, sshConfig)
if err != nil { if err != nil {
output = PrepareError ("Unable to connect", router_ip) output = PrepareError (RouterList, "Unable to connect", router_ip)
allok = false allok = false
} }
...@@ -93,7 +102,7 @@ func SSHRun (router_ip, port, username, password, command, argument, suffix stri ...@@ -93,7 +102,7 @@ func SSHRun (router_ip, port, username, password, command, argument, suffix stri
// create ssh session // create ssh session
session, err := connection.NewSession() session, err := connection.NewSession()
if err != nil { if err != nil {
output = PrepareError ("Connection failed", "failed to create session") output = PrepareError (RouterList, "Connection failed", "failed to create session")
allok = false allok = false
connection.Close() connection.Close()
} else { } else {
...@@ -101,11 +110,11 @@ func SSHRun (router_ip, port, username, password, command, argument, suffix stri ...@@ -101,11 +110,11 @@ func SSHRun (router_ip, port, username, password, command, argument, suffix stri
// run command and save output // run command and save output
rawoutput, err = session.Output(command+" "+argument+suffix) rawoutput, err = session.Output(command+" "+argument+suffix)
if err != nil { if err != nil {
output = PrepareError ("Cannot execute command", command+" "+argument+suffix) output = PrepareError (RouterList, "Cannot execute command", command+" "+argument+suffix)
allok = false allok = false
connection.Close() connection.Close()
} else { } else {
output = PrepareOutput ("Result", ByteToString(rawoutput)) output = PrepareOutput (RouterList, "Result", ByteToString(rawoutput))
connection.Close() connection.Close()
} }
} }
...@@ -114,6 +123,37 @@ func SSHRun (router_ip, port, username, password, command, argument, suffix stri ...@@ -114,6 +123,37 @@ func SSHRun (router_ip, port, username, password, command, argument, suffix stri
return output return output
} }
// get router name from config file
func GetRouterName (RouterId string) string {
var RouterName string
RouterName = viper.GetString(RouterId+".name")
return RouterName
}
// parse enabled router list from config
func ParseRoutersFromConfig (enabled string) []Routers {
var allkeys []string
var routers []Routers
// get all keys from config
allkeys = viper.AllKeys()
sort.Strings(allkeys)
for i := range allkeys {
// find those, who match routerNN.enabled
matched, _ := regexp.MatchString(enabled, allkeys[i])
if matched == true {
//check if enabled = "yes"
var strip = regexp.MustCompile(`\.enabled`)
routerid := strip.ReplaceAllString(allkeys[i], "")
if viper.GetString(routerid+".enabled") == "yes" {
routername := GetRouterName(routerid)
routers = append(routers, Routers { RouterId: routerid, RouterName: routername })
}
}
}
return routers
}
// show page and base logic // show page and base logic
func ShowPage (w http.ResponseWriter, r *http.Request) { func ShowPage (w http.ResponseWriter, r *http.Request) {
// select html template // select html template
...@@ -133,7 +173,7 @@ func ShowPage (w http.ResponseWriter, r *http.Request) { ...@@ -133,7 +173,7 @@ func ShowPage (w http.ResponseWriter, r *http.Request) {
// check for selected router exist // check for selected router exist
if viper.IsSet(input_router+".address") == false { if viper.IsSet(input_router+".address") == false {
output = PrepareError ("Bad router id", input_router) output = PrepareError (RouterList, "Bad router id", input_router)
allok = false allok = false
} else { } else {
// setting router variables from config file // setting router variables from config file
...@@ -149,7 +189,7 @@ func ShowPage (w http.ResponseWriter, r *http.Request) { ...@@ -149,7 +189,7 @@ func ShowPage (w http.ResponseWriter, r *http.Request) {
// check for selected action // check for selected action
if CheckCommand(input_action) == false { if CheckCommand(input_action) == false {
output = PrepareError ("Bad command", input_action) output = PrepareError (RouterList, "Bad command", input_action)
allok = false allok = false
} else { } else {
action = input_action action = input_action
...@@ -157,7 +197,7 @@ func ShowPage (w http.ResponseWriter, r *http.Request) { ...@@ -157,7 +197,7 @@ func ShowPage (w http.ResponseWriter, r *http.Request) {
// check for argument // check for argument
if net.ParseIP(input_argument) == nil { if net.ParseIP(input_argument) == nil {
output = PrepareError ("Bad argument", input_argument) output = PrepareError (RouterList, "Bad argument", input_argument)
allok = false allok = false
} else { } else {
argument = input_argument argument = input_argument
...@@ -179,7 +219,8 @@ func ShowPage (w http.ResponseWriter, r *http.Request) { ...@@ -179,7 +219,8 @@ func ShowPage (w http.ResponseWriter, r *http.Request) {
} }
} else { } else {
// show page without result // show page without result
t.Execute(w, nil) output := PrepareOutput(RouterList, "", "")
t.Execute(w, output)
} }
} }
...@@ -187,6 +228,9 @@ func main() { ...@@ -187,6 +228,9 @@ func main() {
// set config // set config
flag.StringVar(&datapath, "data", "", "data directory") flag.StringVar(&datapath, "data", "", "data directory")
flag.Parse() flag.Parse()
if datapath == "" {
log.Fatal("Please, define \"-data=\" flag (directory with config and template files)")
}
viper.SetConfigName("config") viper.SetConfigName("config")
viper.SetConfigType("toml") viper.SetConfigType("toml")
viper.AddConfigPath(".") viper.AddConfigPath(".")
...@@ -196,6 +240,9 @@ func main() { ...@@ -196,6 +240,9 @@ func main() {
panic(fmt.Errorf("Fatal error config file: %s \n", errconfig)) panic(fmt.Errorf("Fatal error config file: %s \n", errconfig))
} }
// read router list from config
RouterList = ParseRoutersFromConfig(`router[\d]*.enabled`)
// set http server variables // set http server variables
server_address := viper.GetString("server.address") server_address := viper.GetString("server.address")
server_port := viper.GetString("server.port") server_port := viper.GetString("server.port")
......
...@@ -18,9 +18,8 @@ ...@@ -18,9 +18,8 @@
<td style="width: 50px;">&nbsp;</td> <td style="width: 50px;">&nbsp;</td>
<td> <td>
<select name="router"> <select name="router">
<option value="router01">router01</option> {{range .RoutersList}}<option value="{{ .RouterId }}">{{ .RouterName }}</option>
<option value="router02">router02</option> {{else}}<option>no enabled routers</option>{{end}}
<option value="router03">router03</option>
</select> </select>
</td> </td>
</tr> </tr>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment