collectd_plugin/
lib.rs

1//! Collectd is a ubiquitous system statistics collection daemon.
2//! `collectd_plugin` leverages Collectd's ability to dynamically load plugins and
3//! creates an ergonomic, yet extremely low cost abstraction API to interface with
4//! Collectd.
5//!
6//! Features:
7//!
8//! - No unnecessary allocations when submitting / receiving values, logging
9//! - Register multiple plugin instances
10//! - Automatic deserialization of plugin configs via [Serde](https://github.com/serde-rs/serde) (can opt-out)
11//! - Deployment: compile against collectd version and scp to server
12//! - Referenced Rust libraries are statically linked
13//! - Help writing thread safe plugins thanks to the Rust compiler
14//!
15//! ## Usage
16//!
17//! Add to your `Cargo.toml`:
18//!
19//! ```toml
20//! [dependencies]
21//! collectd-plugin = "0.16.0"
22//! ```
23//!
24//! Rust 1.33 or later is needed to build.
25//!
26//! Works with any collectd version 5.4+, but all users will need to specify the collectd api
27//! version they want to target via the `COLLECTD_VERSION` environment variable (or rely on
28//! `$(collectd -h)` or `COLLECTD_PATH` variable).
29//!
30//! | `COLLECTED_VERSION` |  Compatible Range |
31//! |---------------------|-------------------|
32//! | 5.4                 | [5.4, 5.5)        |
33//! | 5.5                 | [5.5, 5.7)        |
34//! | 5.7                 | [5.7,)            |
35//!
36//! ## Quickstart
37//!
38//! Below is a complete plugin that dummy reports [load](https://en.wikipedia.org/wiki/Load_(computing)) values to collectd, as it registers a `READ` hook. For an implementation that reimplements Collectd's own load plugin, see [plugins/load](https://github.com/nickbabcock/collectd-rust-plugin/tree/master/plugins/load)
39//!
40//! ```rust
41//! use collectd_plugin::{
42//!     ConfigItem, Plugin, PluginCapabilities, PluginManager, PluginRegistration, Value,
43//!     ValueListBuilder, collectd_plugin
44//! };
45//! use std::error;
46//!
47//! #[derive(Default)]
48//! struct MyPlugin;
49//!
50//! // A manager decides the name of the family of plugins and also registers one or more plugins based
51//! // on collectd's configuration files
52//! impl PluginManager for MyPlugin {
53//!     // A plugin needs a unique name to be referenced by collectd
54//!     fn name() -> &'static str {
55//!         "myplugin"
56//!     }
57//!
58//!     // Our plugin might have configuration section in collectd.conf, which will be passed here if
59//!     // present. Our contrived plugin doesn't care about configuration so it returns only a single
60//!     // plugin (itself).
61//!     fn plugins(_config: Option<&[ConfigItem]>) -> Result<PluginRegistration, Box<error::Error>> {
62//!         Ok(PluginRegistration::Single(Box::new(MyPlugin)))
63//!     }
64//! }
65//!
66//! impl Plugin for MyPlugin {
67//!     // We define that our plugin will only be reporting / submitting values to writers
68//!     fn capabilities(&self) -> PluginCapabilities {
69//!         PluginCapabilities::READ
70//!     }
71//!
72//!     fn read_values(&self) -> Result<(), Box<error::Error>> {
73//!         // Create a list of values to submit to collectd. We'll be sending in a vector representing the
74//!         // "load" type. Short-term load is first (15.0) followed by mid-term and long-term. The number
75//!         // of values that you submit at a time depends on types.db in collectd configurations
76//!         let values = vec![Value::Gauge(15.0), Value::Gauge(10.0), Value::Gauge(12.0)];
77//!
78//!         // Submit our values to collectd. A plugin can submit any number of times.
79//!         ValueListBuilder::new(Self::name(), "load")
80//!             .values(&values)
81//!             .submit()?;
82//!
83//!         Ok(())
84//!     }
85//! }
86//!
87//! // We pass in our plugin manager type
88//! collectd_plugin!(MyPlugin);
89//!
90//! # fn main() {
91//! # }
92//! ```
93
94#[cfg(feature = "serde")]
95pub mod de;
96
97#[cfg(feature = "serde")]
98pub mod ser;
99
100pub mod bindings;
101pub mod internal;
102#[macro_use]
103mod api;
104mod errors;
105#[macro_use]
106mod plugins;
107
108pub use crate::api::{
109    collectd_log, CdTime, CollectdLogger, CollectdLoggerBuilder, ConfigItem, ConfigValue, LogLevel,
110    MetaValue, Value, ValueList, ValueListBuilder, ValueReport,
111};
112pub use crate::errors::{CacheRateError, ConfigError, ReceiveError, SubmitError};
113pub use crate::plugins::{
114    Plugin, PluginCapabilities, PluginManager, PluginManagerCapabilities, PluginRegistration,
115};
116
117#[cfg(doctest)]
118doc_comment::doctest!("../README.md");