Skip to content

Commit

Permalink
Add meminfo
Browse files Browse the repository at this point in the history
  • Loading branch information
dlopes7 committed Dec 9, 2019
1 parent f491d47 commit 8c86aff
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ Tested with `AIX 7.2` on `IBM POWER 9`

Collectors:

- cpu
- cpu
- memory
64 changes: 64 additions & 0 deletions collector/meminfo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2015 The Prometheus Authors
// Licensed 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.

package collector

import (
"fmt"
"strings"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
)

const (
memInfoSubsystem = "memory"
)

type meminfoCollector struct{}

func init() {
registerCollector("meminfo", true, NewMeminfoCollector)
}

// NewMeminfoCollector returns a new Collector exposing memory stats.
func NewMeminfoCollector() (Collector, error) {
return &meminfoCollector{}, nil
}

// Update calls (*meminfoCollector).getMemInfo to get the platform specific
// memory metrics.
func (c *meminfoCollector) Update(ch chan<- prometheus.Metric) error {
var metricType prometheus.ValueType
memInfo, err := c.getMemInfo()
if err != nil {
return fmt.Errorf("couldn't get meminfo: %s", err)
}
log.Debugf("Set node_mem: %#v", memInfo)
for k, v := range memInfo {
if strings.HasSuffix(k, "_total") {
metricType = prometheus.CounterValue
} else {
metricType = prometheus.GaugeValue
}
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName(namespace, memInfoSubsystem, k),
fmt.Sprintf("Memory information field %s.", k),
nil, nil,
),
metricType, v,
)
}
return nil
}
49 changes: 49 additions & 0 deletions collector/meminfo_aix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package collector

/*
#cgo LDFLAGS: -lperfstat
#include <stdio.h>
#include <libperfstat.h>
#include <stdlib.h>
int getMemInfo(perfstat_memory_total_t *mem_now) {
int rc;
rc = perfstat_memory_total(NULL, mem_now, sizeof(perfstat_memory_total_t), 1);
if (rc <= 0 ) {
return rc;
}
return 0;
}
*/
import "C"
import (
"fmt"
"unsafe"
)

func (c *meminfoCollector) getMemInfo() (map[string]float64, error) {

var memnow C.perfstat_memory_total_t

if _, err := C.getMemInfo(&memnow); err != nil {
return nil, fmt.Errorf("could not collect memory from getMemInfo: %v", err)
}
defer C.free(unsafe.Pointer(&memnow))

// perfstat_memory_total returns data in number of 4k pages
ps := float64(4000)
return map[string]float64{
"real_total_bytes": ps * float64(memnow.real_total),
"real_free_bytes": ps * float64(memnow.real_free),
"real_pinned_bytes": ps * float64(memnow.real_pinned),
"real_inuse_bytes": ps * float64(memnow.real_inuse),
"real_system_bytes": ps * float64(memnow.real_system),
"real_user_bytes": ps * float64(memnow.real_user),
"real_process_bytes": ps * float64(memnow.real_process),
"real_avail_bytes": ps * float64(memnow.real_avail),
"virt_total_bytes": ps * float64(memnow.virt_total),
"virt_active_bytes": ps * float64(memnow.virt_active),
}, nil

}

0 comments on commit 8c86aff

Please sign in to comment.