1/*
2** Zabbix
3** Copyright (C) 2001-2021 Zabbix SIA
4**
5** This program is free software; you can redistribute it and/or modify
6** it under the terms of the GNU General Public License as published by
7** the Free Software Foundation; either version 2 of the License, or
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
15** You should have received a copy of the GNU General Public License
16** along with this program; if not, write to the Free Software
17** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18**/
19
20package file
21
22import (
23	"zabbix.com/pkg/conf"
24	"zabbix.com/pkg/plugin"
25	"zabbix.com/pkg/std"
26)
27
28type Options struct {
29	Timeout  int `conf:"optional,range=1:30"`
30	Capacity int `conf:"optional,range=1:100"`
31}
32
33// Plugin -
34type Plugin struct {
35	plugin.Base
36	options Options
37}
38
39var impl Plugin
40
41// Export -
42func (p *Plugin) Export(key string, params []string, ctx plugin.ContextProvider) (result interface{}, err error) {
43	switch key {
44	case "vfs.file.cksum":
45		return p.exportCksum(params)
46	case "vfs.file.contents":
47		return p.exportContents(params)
48	case "vfs.file.exists":
49		return p.exportExists(params)
50	case "vfs.file.size":
51		return p.exportSize(params)
52	case "vfs.file.time":
53		return p.exportTime(params)
54	case "vfs.file.regexp":
55		return p.exportRegexp(params)
56	case "vfs.file.regmatch":
57		return p.exportRegmatch(params)
58	case "vfs.file.md5sum":
59		return p.exportMd5sum(params)
60	default:
61		return nil, plugin.UnsupportedMetricError
62	}
63}
64
65func (p *Plugin) Configure(global *plugin.GlobalOptions, options interface{}) {
66	if err := conf.Unmarshal(options, &p.options); err != nil {
67		p.Warningf("cannot unmarshal configuration options: %s", err)
68	}
69	if p.options.Timeout == 0 {
70		p.options.Timeout = global.Timeout
71	}
72}
73
74func (p *Plugin) Validate(options interface{}) error {
75	var o Options
76	return conf.Unmarshal(options, &o)
77}
78
79var stdOs std.Os
80
81func init() {
82	stdOs = std.NewOs()
83	plugin.RegisterMetrics(&impl, "File",
84		"vfs.file.cksum", "Returns File checksum, calculated by the UNIX cksum algorithm.",
85		"vfs.file.contents", "Retrieves contents of the file.",
86		"vfs.file.exists", "Returns if file exists or not.",
87		"vfs.file.time", "Returns file time information.",
88		"vfs.file.size", "Returns file size.",
89		"vfs.file.regexp", "Find string in a file.",
90		"vfs.file.regmatch", "Find string in a file.",
91		"vfs.file.md5sum", "Returns MD5 checksum of file.")
92}
93