rob05c commented on a change in pull request #2305: Add TO Go cdns/health and 
cdns/{name}/health
URL: https://github.com/apache/trafficcontrol/pull/2305#discussion_r324730642
 
 

 ##########
 File path: traffic_ops/traffic_ops_golang/util/monitorhlp/monitorhlp.go
 ##########
 @@ -0,0 +1,116 @@
+package monitorhlp
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import (
+       "database/sql"
+       "encoding/json"
+       "errors"
+       "net/http"
+       "net/url"
+       "strconv"
+       "time"
+
+       "github.com/apache/trafficcontrol/lib/go-tc"
+       
"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/dbhelpers"
+)
+
+const MonitorProxyParameter = "tm.traffic_mon_fwd_proxy"
+const MonitorRequestTimeout = time.Second * 10
+const MonitorOnlineStatus = "ONLINE"
+
+// GetClient returns the http.Client for making requests to the Traffic 
Monitor. This should always be used, rather than creating a default 
http.Client, to ensure any monitor forward proxy parameter is used correctly.
+func GetClient(tx *sql.Tx) (*http.Client, error) {
+       monitorForwardProxy, monitorForwardProxyExists, err := 
dbhelpers.GetGlobalParam(tx, MonitorProxyParameter)
+       if err != nil {
+               return nil, errors.New("getting global monitor proxy parameter: 
" + err.Error())
+       }
+       client := &http.Client{Timeout: MonitorRequestTimeout}
+       if monitorForwardProxyExists {
+               proxyURI, err := url.Parse(monitorForwardProxy)
+               if err != nil {
+                       return nil, errors.New("monitor forward proxy '" + 
monitorForwardProxy + "' in parameter '" + MonitorProxyParameter + "' not a 
URI: " + err.Error())
+               }
+               client = &http.Client{Timeout: MonitorRequestTimeout, 
Transport: &http.Transport{Proxy: http.ProxyURL(proxyURI)}}
+       }
+       return client, nil
+}
+
+// GetURLs returns an FQDN, including port, of an online monitor for each CDN. 
If a CDN has no online monitors, that CDN will not have an entry in the map. If 
a CDN has multiple online monitors, an arbitrary one will be returned.
+func GetURLs(tx *sql.Tx) (map[tc.CDNName]string, error) {
+       rows, err := tx.Query(`
+SELECT s.host_name, s.domain_name, s.tcp_port, c.name as cdn
+FROM server as s
+JOIN type as t ON s.type = t.id
+JOIN status as st ON st.id = s.status
+JOIN cdn as c ON c.id = s.cdn_id
+WHERE t.name = '` + tc.MonitorTypeName + `'
+AND st.name = '` + MonitorOnlineStatus + `'
+`)
+       if err != nil {
+               return nil, errors.New("querying monitors: " + err.Error())
+       }
+       defer rows.Close()
+       monitors := map[tc.CDNName]string{}
+       for rows.Next() {
+               host := ""
+               domain := ""
+               port := sql.NullInt64{}
+               cdn := ""
+               if err := rows.Scan(&host, &domain, &port, &cdn); err != nil {
+                       return nil, errors.New("scanning monitors: " + 
err.Error())
+               }
+               fqdn := host + "." + domain
+               if port.Valid {
+                       fqdn += ":" + strconv.FormatInt(port.Int64, 10)
+               }
+               monitors[tc.CDNName(cdn)] = fqdn
+       }
+       return monitors, nil
+}
+
+func GetCRStates(monitorFQDN string, client *http.Client) (tc.CRStates, error) 
{
 
 Review comment:
   Yes, they were copy-pasted, because the PRs were open at the same time, with 
the intention of combing them after this was merged to master. Good catch. I'll 
do that now.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to