bosun.org@v0.0.0-20210513094433-e25bc3e69a1f/cmd/scollector/collectors/hbase_unix.go (about)

     1  // +build darwin linux
     2  // note: this collector only works on hbase 1.0+
     3  
     4  package collectors
     5  
     6  import (
     7  	"encoding/json"
     8  	"math"
     9  	"net/http"
    10  	"regexp"
    11  	"strings"
    12  
    13  	"bosun.org/cmd/scollector/conf"
    14  	"bosun.org/metadata"
    15  	"bosun.org/opentsdb"
    16  )
    17  
    18  var (
    19  	hbURL    = "/jmx?qry=Hadoop:service=HBase,name=RegionServer,sub=Server"
    20  	hbRegURL = "/jmx?qry=Hadoop:service=HBase,name=RegionServer,sub=Regions"
    21  	hbRepURL = "/jmx?qry=Hadoop:service=HBase,name=RegionServer,sub=Replication"
    22  	hbICPURL = "/jmx?qry=Hadoop:service=HBase,name=RegionServer,sub=IPC"
    23  	hbGCURL  = "/jmx?qry=java.lang:type=GarbageCollector,name=*"
    24  )
    25  
    26  func init() {
    27  	registerInit(func(c *conf.Conf) {
    28  		host := ""
    29  		if c.HadoopHost != "" {
    30  			host = "http://" + c.HadoopHost
    31  		} else {
    32  			host = "http://localhost:60030"
    33  		}
    34  		hbURL = host + hbURL
    35  		hbRegURL = host + hbRegURL
    36  		hbRepURL = host + hbRepURL
    37  		hbGCURL = host + hbGCURL
    38  		hbICPURL = host + hbICPURL
    39  		collectors = append(collectors, &IntervalCollector{F: c_hbase_region, Enable: enableURL(hbURL)})
    40  		if c.HbaseRegions {
    41  			collectors = append(collectors, &IntervalCollector{F: c_hbase_regions, Enable: enableURL(hbRegURL)})
    42  		}
    43  		collectors = append(collectors, &IntervalCollector{F: c_hbase_replication, Enable: enableURL(hbRepURL)})
    44  		collectors = append(collectors, &IntervalCollector{F: c_hbase_gc, Enable: enableURL(hbGCURL)})
    45  		collectors = append(collectors, &IntervalCollector{F: c_hbase_ipc, Enable: enableURL(hbICPURL)})
    46  	})
    47  }
    48  
    49  type jmx struct {
    50  	Beans []map[string]interface{} `json:"beans"`
    51  }
    52  
    53  func getBeans(url string, jmx *jmx) error {
    54  	res, err := http.Get(url)
    55  	if err != nil {
    56  		return err
    57  	}
    58  	defer res.Body.Close()
    59  	if err := json.NewDecoder(res.Body).Decode(&jmx); err != nil {
    60  		return err
    61  	}
    62  	return nil
    63  }
    64  
    65  func c_hbase_region() (opentsdb.MultiDataPoint, error) {
    66  	var j jmx
    67  	if err := getBeans(hbURL, &j); err != nil {
    68  		return nil, err
    69  	}
    70  	var md opentsdb.MultiDataPoint
    71  	if len(j.Beans) > 0 && len(j.Beans[0]) > 0 {
    72  		for k, v := range j.Beans[0] {
    73  			if vv, ok := v.(float64); ok {
    74  				if vv < math.MaxInt64 {
    75  					Add(&md, "hbase.region."+k, v, nil, metadata.Unknown, metadata.None, "")
    76  				}
    77  			}
    78  		}
    79  	}
    80  	return md, nil
    81  }
    82  
    83  func c_hbase_ipc() (opentsdb.MultiDataPoint, error) {
    84  	var j jmx
    85  	if err := getBeans(hbICPURL, &j); err != nil {
    86  		return nil, err
    87  	}
    88  	var md opentsdb.MultiDataPoint
    89  	if len(j.Beans) > 0 {
    90  		for k, v := range j.Beans[0] {
    91  			if vv, ok := v.(float64); ok {
    92  				if vv < math.MaxInt64 {
    93  					Add(&md, "hbase.ipc."+k, v, nil, metadata.Unknown, metadata.None, "")
    94  				}
    95  			}
    96  		}
    97  	}
    98  	return md, nil
    99  }
   100  
   101  func c_hbase_regions() (opentsdb.MultiDataPoint, error) {
   102  	var j jmx
   103  	if err := getBeans(hbRegURL, &j); err != nil {
   104  		return nil, err
   105  	}
   106  	var md opentsdb.MultiDataPoint
   107  	if len(j.Beans) > 0 && len(j.Beans[0]) > 0 {
   108  		for k, v := range j.Beans[0] {
   109  			if vv, ok := v.(float64); ok {
   110  				if vv > math.MaxInt64 {
   111  					continue
   112  				}
   113  				kParts := strings.Split(k, "_")
   114  				t := make(opentsdb.TagSet)
   115  				var m string
   116  				for i := 0; i+1 < len(kParts); i += 2 {
   117  					if kParts[i] == "metric" {
   118  						m = strings.Join(kParts[i+1:], "_")
   119  						break
   120  					}
   121  					t[kParts[i]] = kParts[i+1]
   122  				}
   123  				if m == "" {
   124  					continue
   125  				}
   126  				Add(&md, "hbase.regions."+m, v, t, metadata.Unknown, metadata.None, "")
   127  			}
   128  		}
   129  	}
   130  	return md, nil
   131  }
   132  
   133  func c_hbase_gc() (opentsdb.MultiDataPoint, error) {
   134  	var j jmx
   135  	if err := getBeans(hbGCURL, &j); err != nil {
   136  		return nil, err
   137  	}
   138  	var md opentsdb.MultiDataPoint
   139  	const metric = "hbase.region.gc."
   140  	for _, bean := range j.Beans {
   141  		if name, ok := bean["Name"].(string); ok && name != "" {
   142  			ts := opentsdb.TagSet{"name": name}
   143  			for k, v := range bean {
   144  				if vv, ok := v.(float64); ok {
   145  					if vv < math.MaxInt64 {
   146  						switch k {
   147  						case "CollectionCount":
   148  							Add(&md, metric+k, v, ts, metadata.Counter, metadata.Count, "A counter for the number of times that garbage collection has been called.")
   149  						case "CollectionTime":
   150  							Add(&md, metric+k, v, ts, metadata.Counter, metadata.None, "The total amount of time spent in garbage collection.")
   151  						}
   152  					}
   153  				}
   154  			}
   155  		}
   156  	}
   157  	return md, nil
   158  }
   159  
   160  func c_hbase_replication() (opentsdb.MultiDataPoint, error) {
   161  	var j jmx
   162  	if err := getBeans(hbRepURL, &j); err != nil {
   163  		return nil, err
   164  	}
   165  	excludeReg, err := regexp.Compile("source.\\d")
   166  	if err != nil {
   167  		return nil, err
   168  	}
   169  	var md opentsdb.MultiDataPoint
   170  	for _, section := range j.Beans {
   171  		for k, v := range section {
   172  			// source.[0-9] entries are for other hosts in the cluster
   173  			if excludeReg.MatchString(k) {
   174  				continue
   175  			}
   176  			// Strip "source." and "sink." from the metric names.
   177  			shortName := strings.TrimPrefix(k, "source.")
   178  			shortName = strings.TrimPrefix(shortName, "sink.")
   179  			metric := "hbase.region." + shortName
   180  			if vv, ok := v.(float64); ok {
   181  				if vv < math.MaxInt64 {
   182  					Add(&md, metric, v, nil, metadata.Unknown, metadata.None, "")
   183  				}
   184  			}
   185  		}
   186  	}
   187  	return md, nil
   188  }