summaryrefslogtreecommitdiffstats
path: root/web/input/doc/faq/private-c-api.md
diff options
context:
space:
mode:
Diffstat (limited to 'web/input/doc/faq/private-c-api.md')
-rw-r--r--web/input/doc/faq/private-c-api.md116
1 files changed, 116 insertions, 0 deletions
diff --git a/web/input/doc/faq/private-c-api.md b/web/input/doc/faq/private-c-api.md
new file mode 100644
index 0000000..585ca42
--- /dev/null
+++ b/web/input/doc/faq/private-c-api.md
@@ -0,0 +1,116 @@
1title: Private C APIs
2parent: FAQ
3---
4
5# Private C APIs
6
7This page describes the Nagios Plugins routines that can be accessed from the
8internal library.
9
10This page is in development, so these are not guaranteed to be available. As
11the API matures and is available in libraries, this information will be
12migrated to the [Development Guidelines][guidelines].
13
14## Basic Functions
15
16### np\_init(char \*plugin\_name, int argc, char \*\*argv)
17
18Initialize the Nagios Plugins routines. Pass the plugin name and `argc` and
19`argv` from `main()`.
20
21A variable `nagios_plugin` will be created for internal use.
22
23### np\_set\_args(int argc, char \*\*argv)
24
25Sets the internally held `argc` and `argv` values.
26
27Shouldn't really need this, but due to `np_extra_opts()`, this is set after
28that call.
29
30### np\_cleanup(void)
31
32Used to clean up allocated memory by the `nagios_plugin` variable. This is
33called by the `die()` routine before exiting.
34
35## State Information
36
37Saving and restoring state allows a plugin to know the last results and thus
38work out differences. This is especially useful when a plugin is capturing
39counter information, which increases with every request.
40
41This currently works by saving state information to a file, though the API
42doesn't care what the backend implementation is.
43
44*Note:* Binary data is not currently supported.
45
46Some things to be aware of, if you use state information:
47
48* There will be problems if a remote host is checked from two different Nagios
49 instances, as the state file on the remote host will be updated twice as
50 often.
51* Binary data may not restore on a program compiled with different options
52 from the program that saved it (e.g., 32 or 64 bit).
53* Binary data may include a structure containing a pointer. Pointer values
54 may not be used in the reading program - i.e., you need to overwrite the
55 value with something `malloc(3)`ed in the current run of the program.
56* State files could be left lying around. We recommend you run a regular job
57 to remove unmodified state files older than 1 week.
58
59### np\_enable\_state(char \*keyname, int data\_version)
60
61Enables the plugin state retention routines. Will set the filename for the
62state file to be `.../{keyname}`.
63
64The `keyname` will have any non alphanumerics replaced with "`_`".
65
66If `keyname` is `NULL`, will generate an SHA1 `keyname` based on the `argv` of
67the plugin (using the [extra-opts][extra-opts] parsed version, if applicable).
68
69*Note:* The `keyname` should be uniquely defined for a particular service, to
70avoid a second invocation of the plugin to use the state information from a
71different invocation. If in doubt, set `keyname=NULL` and allow the routine
72to calculate the `keyname`.
73
74### np\_state\_read(void)
75
76Reads the state file and returns a `state_data` variable.
77
78This routine will call `die()` with `UNKNOWN` if:
79
80* There was a problem reading the state file.
81
82Returns `NULL` if:
83
84* No state file exists - this is possible on the first run.
85* The state file format (internally held by the plugin) does not match.
86* The state data format (passed in `np_enable_state()`) does not match.
87
88Your plugin should always check for `NULL`. It is recommended that your
89plugin returns `OK` on `NULL` as this is similar to a "first run".
90
91If valid data was read, a pointer will be returned which points to a struct
92of:
93
94 typedef struct state_data_struct {
95 time_t time;
96 void *data;
97 int length; /* Of binary data. */
98 } state_data;
99
100### np\_state\_write\_string(time\_t data\_time, char \*string)
101
102If `data_time==0`, use current time. Creates state file, with state format
103version. Writes data version, time, and data. Creates a temporary file and
104then renames into place. There is a possible loss of data if two things
105writing to same key at same time, but there will not be a corrupted state
106file.
107
108### np\_state\_write\_binary(time\_t data\_time, char \*start, int length)
109
110Same as `np_state_write_string()`, but writes binary data. *Currently
111unimplemented.*
112
113[guidelines]: doc/guidelines.html "Nagios Plugin Development Guidelines"
114[extra-opts]: doc/extra-opts.html "Extra-Opts"
115
116<!--% # vim:set filetype=markdown textwidth=78 joinspaces: # %-->