From a0686b7d2bca54b6c2743605eedeb42312ce7484 Mon Sep 17 00:00:00 2001 From: James Shubin Date: Sun, 5 Mar 2017 23:00:46 -0500 Subject: [PATCH] pgraph: graphviz: Update Graphviz lib to quote names properly This also moves the library to after the graph starts so that the kind fields will be visible. --- lib/cli.go | 2 +- lib/main.go | 22 +++++++++++++--------- pgraph/graphviz.go | 14 +++++++++----- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/lib/cli.go b/lib/cli.go index 76a9dbfe..5c0cb0e2 100644 --- a/lib/cli.go +++ b/lib/cli.go @@ -241,7 +241,7 @@ func CLI(program, version string, flags Flags) error { }, cli.StringFlag{ Name: "graphviz-filter, gf", - Value: "dot", // directed graph default + Value: "", Usage: "graphviz filter to use", }, cli.IntFlag{ diff --git a/lib/main.go b/lib/main.go index 778fe6c4..7a0f762a 100644 --- a/lib/main.go +++ b/lib/main.go @@ -483,15 +483,6 @@ func (obj *Main) Run() error { // TODO: do we want to do a transitive reduction? // FIXME: run a type checker that verifies all the send->recv relationships - log.Printf("Graph: %v", G) // show graph - if obj.GraphvizFilter != "" { - if err := G.ExecGraphviz(obj.GraphvizFilter, obj.Graphviz); err != nil { - log.Printf("Graphviz: %v", err) - } else { - log.Printf("Graphviz: Successfully generated graph!") - } - } - // Call this here because at this point the graph does not // know anything about the prometheus instance. if err := prom.UpdatePgraphStartTime(); err != nil { @@ -504,6 +495,19 @@ func (obj *Main) Run() error { // even got going, thus causing nil pointer errors G.Start(first) // sync converger.Start() // after G.Start() + + log.Printf("Graph: %v", G) // show graph + if obj.Graphviz != "" { + filter := obj.GraphvizFilter + if filter == "" { + filter = "dot" // directed graph default + } + if err := G.ExecGraphviz(filter, obj.Graphviz, hostname); err != nil { + log.Printf("Graphviz: %v", err) + } else { + log.Printf("Graphviz: Successfully generated graph!") + } + } first = false } }() diff --git a/pgraph/graphviz.go b/pgraph/graphviz.go index f1e3e0ac..ae88b29a 100644 --- a/pgraph/graphviz.go +++ b/pgraph/graphviz.go @@ -15,7 +15,7 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . -package pgraph +package pgraph // TODO: this should be a subpackage import ( "fmt" @@ -46,14 +46,14 @@ func (g *Graph) Graphviz() (out string) { //out += "\tnode [shape=box];\n" str := "" for i := range g.Adjacency { // reverse paths - out += fmt.Sprintf("\t%s [label=\"%s[%s]\"];\n", i.GetName(), i.Kind(), i.GetName()) + out += fmt.Sprintf("\t\"%s\" [label=\"%s[%s]\"];\n", i.GetName(), i.Kind(), i.GetName()) for j := range g.Adjacency[i] { k := g.Adjacency[i][j] // use str for clearer output ordering if k.Notify { - str += fmt.Sprintf("\t%s -> %s [label=%s,style=bold];\n", i.GetName(), j.GetName(), k.Name) + str += fmt.Sprintf("\t\"%s\" -> \"%s\" [label=\"%s\",style=bold];\n", i.GetName(), j.GetName(), k.Name) } else { - str += fmt.Sprintf("\t%s -> %s [label=%s];\n", i.GetName(), j.GetName(), k.Name) + str += fmt.Sprintf("\t\"%s\" -> \"%s\" [label=\"%s\"];\n", i.GetName(), j.GetName(), k.Name) } } } @@ -64,7 +64,7 @@ func (g *Graph) Graphviz() (out string) { // ExecGraphviz writes out the graphviz data and runs the correct graphviz // filter command. -func (g *Graph) ExecGraphviz(program, filename string) error { +func (g *Graph) ExecGraphviz(program, filename, hostname string) error { switch program { case "dot", "neato", "twopi", "circo", "fdp": @@ -76,6 +76,10 @@ func (g *Graph) ExecGraphviz(program, filename string) error { return fmt.Errorf("no filename given") } + if hostname != "" { + filename = fmt.Sprintf("%s@%s", filename, hostname) + } + // run as a normal user if possible when run with sudo uid, err1 := strconv.Atoi(os.Getenv("SUDO_UID")) gid, err2 := strconv.Atoi(os.Getenv("SUDO_GID"))