|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "html/template" |
| 6 | + "net/http" |
| 7 | + "runtime" |
| 8 | + "runtime/debug" |
| 9 | + |
| 10 | + "github.com/prometheus/common/version" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + docsUrl = "https://github.com/peekjef72/nrpe_exporter#readme" |
| 15 | + templates = ` |
| 16 | + {{ define "page" -}} |
| 17 | + <html> |
| 18 | + <head> |
| 19 | + <title>Prometheus {{ .ExporterName }}</title> |
| 20 | + <style type="text/css"> |
| 21 | + body { margin: 0; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; line-height: 1.42857143; color: #333; background-color: #fff; } |
| 22 | + .navbar { display: flex; background-color: #222; margin: 0; border-width: 0 0 1px; border-style: solid; border-color: #080808; } |
| 23 | + .navbar > * { margin: 0; padding: 15px; } |
| 24 | + .navbar * { line-height: 20px; color: #9d9d9d; } |
| 25 | + .navbar a { text-decoration: none; } |
| 26 | + .navbar a:hover, .navbar a:focus { color: #fff; } |
| 27 | + .navbar-header { font-size: 18px; } |
| 28 | + body > * { margin: 15px; padding: 0; } |
| 29 | + pre { padding: 10px; font-size: 13px; background-color: #f5f5f5; border: 1px solid #ccc; } |
| 30 | + h1, h2 { font-weight: 500; } |
| 31 | + a { color: #337ab7; } |
| 32 | + a:hover, a:focus { color: #23527c; } |
| 33 | + table { border: 1px solid #edd2e6; border-collapse: collapse; margin-bottom: 1rem; width: 80%; } |
| 34 | + tr { border: 1px solid #edd2e6; padding: 0.3rem; text-align: left; width: 35%; } |
| 35 | + th { border: 1px solid #edd2e6; padding: 0.3rem; } |
| 36 | + td { border: 1px solid #edd2e6; padding: 0.3rem; } |
| 37 | + .odd { background-color: rgba(0,0,0,.05); } |
| 38 | + </style> |
| 39 | + </head> |
| 40 | + <body> |
| 41 | + <div class="navbar"> |
| 42 | + <div class="navbar-header"><a href="/">Prometheus {{ .ExporterName }}</a></div> |
| 43 | + <div><a href="/healthz">Health</a></div> |
| 44 | + <div><a href="{{ .ExportPath }}?command=check_load&target=127.0.0.1:5666&metric_name=nrpe_load">Export</a></div> |
| 45 | + <div><a href="{{ .ProfilePath }}">Profiles</a></div> |
| 46 | + <div><a href="/status">Status</a></div> |
| 47 | + <div><a href="{{ .MetricsPath }}">Exporter Metrics</a></div> |
| 48 | + <div><a href="{{ .DocsUrl }}">Help</a></div> |
| 49 | + </div> |
| 50 | + {{template "content" .}} |
| 51 | + </body> |
| 52 | + </html> |
| 53 | + {{- end }} |
| 54 | +
|
| 55 | + {{ define "content.home" -}} |
| 56 | + <h1>This is a <a href="{{ .DocsUrl }}">Prometheus {{ .ExporterName }}</a> instance.</h1> |
| 57 | + <p>You are probably looking for its metrics:</p> |
| 58 | + <p><strong>E.G.:</strong></p> |
| 59 | + <li>nrpe without ssl and no parameter local check_load: <a href="{{ .ExportPath }}?command=check_load&target=127.0.0.1:5666&metric_name=nrpe_load">check_load against localhost:5666 NO SSL</a>.</li> |
| 60 | + <li>nrpe with ssl and no parameter local check_load: <a href="{{ .ExportPath }}?ssl=true&command=check_load&target=127.0.0.1:5666&metric_name=nrpe_load&result_message=true&performance=true">check_load against localhost:5666 SSL</a>.</li> |
| 61 | + <li>nrpe with ssl and allow parameters local check_load: <a href="{{ .ExportPath }}?ssl=true&command=check_load¶ms=params=-r%20-w%20.15,.10,.05%20-c%20.30,.25,.20&target=127.0.0.1:5666&metric_name=nrpe_load&result_message=true&performance=true">check_load against localhost:5666 SSL ALLOW param</a>.</li> |
| 62 | + {{- end }} |
| 63 | +
|
| 64 | + {{ define "content.profiles" -}} |
| 65 | + <h2>Profiles</h2> |
| 66 | + <pre>{{ .Profiles }}</pre> |
| 67 | + {{- end }} |
| 68 | +
|
| 69 | + {{ define "content.status" -}} |
| 70 | + <h2>Build Information</h2> |
| 71 | + <table> |
| 72 | + <tbody> |
| 73 | + <tr class="odd" > |
| 74 | + <th>Version</th> |
| 75 | + <td>{{ .Version.Version }}</td> |
| 76 | + </tr> |
| 77 | + <tr> |
| 78 | + <th>Revision</th> |
| 79 | + <td>{{ .Version.Revision }}</td> |
| 80 | + </tr> |
| 81 | + <tr class="odd" > |
| 82 | + <th>Branch</th> |
| 83 | + <td>{{ .Version.Branch }}</td> |
| 84 | + </tr> |
| 85 | + <tr> |
| 86 | + <th>BuildUser</th> |
| 87 | + <td>{{ .Version.BuildUser }}</td> |
| 88 | + </tr> |
| 89 | + <tr class="odd" > |
| 90 | + <th>BuildDate</th> |
| 91 | + <td>{{ .Version.BuildDate }}</td> |
| 92 | + </tr> |
| 93 | + <tr> |
| 94 | + <th>BuildTags</th> |
| 95 | + <td>{{ .Version.BuildTags }}</td> |
| 96 | + </tr> |
| 97 | + <tr class="odd" > |
| 98 | + <th>GoVersion</th> |
| 99 | + <td>{{ .Version.GoVersion }}</td> |
| 100 | + </tr> |
| 101 | + </tbody> |
| 102 | + </table> |
| 103 | + {{- end }} |
| 104 | +
|
| 105 | + {{ define "content.error" -}} |
| 106 | + <h2>Error</h2> |
| 107 | + <pre>{{ .Err }}</pre> |
| 108 | + {{- end }} |
| 109 | + ` |
| 110 | +) |
| 111 | + |
| 112 | +type versionInfo struct { |
| 113 | + Version string |
| 114 | + Revision string |
| 115 | + Branch string |
| 116 | + BuildUser string |
| 117 | + BuildDate string |
| 118 | + BuildTags string |
| 119 | + GoVersion string |
| 120 | +} |
| 121 | +type tdata struct { |
| 122 | + ExporterName string |
| 123 | + ExportPath string |
| 124 | + MetricsPath string |
| 125 | + ProfilePath string |
| 126 | + DocsUrl string |
| 127 | + |
| 128 | + // `/profiles` only |
| 129 | + Profiles string |
| 130 | + |
| 131 | + // status |
| 132 | + Version versionInfo |
| 133 | + // `/error` only |
| 134 | + Err error |
| 135 | +} |
| 136 | + |
| 137 | +var ( |
| 138 | + allTemplates = template.Must(template.New("").Parse(templates)) |
| 139 | + homeTemplate = pageTemplate("home") |
| 140 | + profileTemplate = pageTemplate("profiles") |
| 141 | + statusTemplate = pageTemplate("status") |
| 142 | + errorTemplate = pageTemplate("error") |
| 143 | +) |
| 144 | + |
| 145 | +func pageTemplate(name string) *template.Template { |
| 146 | + pageTemplate := fmt.Sprintf(`{{define "content"}}{{template "content.%s" .}}{{end}}{{template "page" .}}`, name) |
| 147 | + return template.Must(template.Must(allTemplates.Clone()).Parse(pageTemplate)) |
| 148 | +} |
| 149 | + |
| 150 | +// HomeHandlerFunc is the HTTP handler for the home page (`/`). |
| 151 | +func HomeHandlerFunc(exporter Exporter) func(http.ResponseWriter, *http.Request) { |
| 152 | + return func(w http.ResponseWriter, r *http.Request) { |
| 153 | + homeTemplate.Execute(w, &tdata{ |
| 154 | + ExporterName: exporter.ExporterName, |
| 155 | + ExportPath: exporter.ExporterPath, |
| 156 | + MetricsPath: exporter.MetricPath, |
| 157 | + ProfilePath: exporter.ProfilesPath, |
| 158 | + DocsUrl: docsUrl, |
| 159 | + }) |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +// ConfigHandlerFunc is the HTTP handler for the `/config` page. It outputs the configuration marshaled in YAML format. |
| 164 | +func ProfilesHandlerFunc(exporter Exporter) func(http.ResponseWriter, *http.Request) { |
| 165 | + return func(w http.ResponseWriter, r *http.Request) { |
| 166 | + profiles, err := exporter.Profiles.Dump() |
| 167 | + if err != nil { |
| 168 | + HandleError(0, err, exporter, w, r) |
| 169 | + return |
| 170 | + } |
| 171 | + profileTemplate.Execute(w, &tdata{ |
| 172 | + ExporterName: exporter.ExporterName, |
| 173 | + ExportPath: exporter.ExporterPath, |
| 174 | + MetricsPath: exporter.MetricPath, |
| 175 | + ProfilePath: exporter.ProfilesPath, |
| 176 | + DocsUrl: docsUrl, |
| 177 | + Profiles: profiles, |
| 178 | + }) |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +// ConfigHandlerFunc is the HTTP handler for the `/config` page. It outputs the configuration marshaled in YAML format. |
| 183 | +func StatusHandlerFunc(exporter Exporter) func(http.ResponseWriter, *http.Request) { |
| 184 | + return func(w http.ResponseWriter, r *http.Request) { |
| 185 | + |
| 186 | + vinfos := versionInfo{ |
| 187 | + Version: version.Version, |
| 188 | + Revision: version.Revision, |
| 189 | + Branch: version.Branch, |
| 190 | + BuildUser: version.BuildUser, |
| 191 | + BuildDate: version.BuildDate, |
| 192 | + BuildTags: computeTags(), |
| 193 | + GoVersion: runtime.Version(), |
| 194 | + } |
| 195 | + |
| 196 | + statusTemplate.Execute(w, &tdata{ |
| 197 | + ExporterName: exporter.ExporterName, |
| 198 | + ExportPath: exporter.ExporterPath, |
| 199 | + MetricsPath: exporter.MetricPath, |
| 200 | + ProfilePath: exporter.ProfilesPath, |
| 201 | + DocsUrl: docsUrl, |
| 202 | + Version: vinfos, |
| 203 | + }) |
| 204 | + } |
| 205 | +} |
| 206 | + |
| 207 | +// HandleError is an error handler that other handlers defer to in case of error. It is important to not have written |
| 208 | +// anything to w before calling HandleError(), or the 500 status code won't be set (and the content might be mixed up). |
| 209 | +func HandleError(status int, err error, exporter Exporter, w http.ResponseWriter, r *http.Request) { |
| 210 | + if status == 0 { |
| 211 | + status = http.StatusInternalServerError |
| 212 | + } |
| 213 | + w.WriteHeader(status) |
| 214 | + errorTemplate.Execute(w, &tdata{ |
| 215 | + ExporterName: exporter.ExporterName, |
| 216 | + ExportPath: exporter.ExporterPath, |
| 217 | + MetricsPath: exporter.MetricPath, |
| 218 | + ProfilePath: exporter.ProfilesPath, |
| 219 | + DocsUrl: docsUrl, |
| 220 | + Err: err, |
| 221 | + }) |
| 222 | +} |
| 223 | + |
| 224 | +func computeTags() string { |
| 225 | + var ( |
| 226 | + tags = "unknown" |
| 227 | + ) |
| 228 | + |
| 229 | + buildInfo, ok := debug.ReadBuildInfo() |
| 230 | + if !ok { |
| 231 | + return tags |
| 232 | + } |
| 233 | + for _, v := range buildInfo.Settings { |
| 234 | + if v.Key == "-tags" { |
| 235 | + tags = v.Value |
| 236 | + } |
| 237 | + } |
| 238 | + return tags |
| 239 | +} |
0 commit comments