Welcome to mirror list, hosted at ThFree Co, Russian Federation.

fs.go « fs « vfs « plugins « go « src - github.com/zabbix/zabbix.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2b609e202ee4104ccbfbccacbf09764d83adee31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// +build windows

/*
** Zabbix
** Copyright (C) 2001-2020 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
**/

package vfsfs

import (
	"encoding/json"
	"errors"

	"zabbix.com/pkg/plugin"
)

const (
	errorInvalidParameters = "Invalid number of parameters."
)

const (
	statModeTotal = iota
	statModeFree
	statModeUsed
	statModePFree
	statModePUsed
)

type FsStats struct {
	Total uint64  `json:"total"`
	Free  uint64  `json:"free"`
	Used  uint64  `json:"used"`
	PFree float64 `json:"pfree"`
	PUsed float64 `json:"pused"`
}

type FsInfo struct {
	FsName    *string  `json:"{#FSNAME},omitempty"`
	FsType    *string  `json:"{#FSTYPE},omitempty"`
	DriveType *string  `json:"{#FSDRIVETYPE},omitempty"`
	Bytes     *FsStats `json:"bytes,omitempty"`
}

type Plugin struct {
	plugin.Base
}

var impl Plugin

func (p *Plugin) exportDiscovery(params []string) (value interface{}, err error) {
	if len(params) != 0 {
		return nil, errors.New(errorInvalidParameters)
	}
	var d []*FsInfo
	if d, err = p.getFsInfo(); err != nil {
		return
	}
	var b []byte
	if b, err = json.Marshal(&d); err != nil {
		return
	}
	return string(b), nil
}

func (p *Plugin) exportGet(params []string) (value interface{}, err error) {
	if len(params) != 0 {
		return nil, errors.New(errorInvalidParameters)
	}
	var d []*FsInfo
	if d, err = p.getFsInfoStats(); err != nil {
		return
	}
	var b []byte
	if b, err = json.Marshal(&d); err != nil {
		return
	}
	return string(b), nil
}

func (p *Plugin) exportSize(params []string) (value interface{}, err error) {
	if len(params) < 1 || params[0] == "" {
		return nil, errors.New("Invalid first parameter.")
	}
	if len(params) > 2 {
		return nil, errors.New("Too many parameters.")
	}
	mode := statModeTotal
	if len(params) == 2 {
		switch params[1] {
		case "total":
		case "free":
			mode = statModeFree
		case "used":
			mode = statModeUsed
		case "pfree":
			mode = statModePFree
		case "pused":
			mode = statModePUsed
		default:
			return nil, errors.New("Invalid second parameter.")
		}
	}
	var stats *FsStats
	if stats, err = getFsStats(params[0]); err != nil {
		return
	}

	switch mode {
	case statModeTotal:
		return stats.Total, nil
	case statModeFree:
		return stats.Free, nil
	case statModeUsed:
		return stats.Used, nil
	case statModePFree:
		return stats.PFree, nil
	case statModePUsed:
		return stats.PUsed, nil
	}

	return nil, errors.New("Invalid second parameter.")
}

func (p *Plugin) Export(key string, params []string, ctx plugin.ContextProvider) (result interface{}, err error) {
	switch key {
	case "vfs.fs.discovery":
		return p.exportDiscovery(params)
	case "vfs.fs.get":
		return p.exportGet(params)
	case "vfs.fs.size":
		return p.exportSize(params)
	default:
		return nil, plugin.UnsupportedMetricError
	}
}

func init() {
	plugin.RegisterMetrics(&impl, "VfsFs",
		"vfs.fs.discovery", "List of mounted filesystems. Used for low-level discovery.",
		"vfs.fs.get", "List of mounted filesystems with statistics.",
		"vfs.fs.size", "Disk space in bytes or in percentage from total.",
	)
}