diff options
Diffstat (limited to 'web/input/doc/faq/private-c-api.md')
-rw-r--r-- | web/input/doc/faq/private-c-api.md | 116 |
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 @@ | |||
1 | title: Private C APIs | ||
2 | parent: FAQ | ||
3 | --- | ||
4 | |||
5 | # Private C APIs | ||
6 | |||
7 | This page describes the Nagios Plugins routines that can be accessed from the | ||
8 | internal library. | ||
9 | |||
10 | This page is in development, so these are not guaranteed to be available. As | ||
11 | the API matures and is available in libraries, this information will be | ||
12 | migrated to the [Development Guidelines][guidelines]. | ||
13 | |||
14 | ## Basic Functions | ||
15 | |||
16 | ### np\_init(char \*plugin\_name, int argc, char \*\*argv) | ||
17 | |||
18 | Initialize the Nagios Plugins routines. Pass the plugin name and `argc` and | ||
19 | `argv` from `main()`. | ||
20 | |||
21 | A variable `nagios_plugin` will be created for internal use. | ||
22 | |||
23 | ### np\_set\_args(int argc, char \*\*argv) | ||
24 | |||
25 | Sets the internally held `argc` and `argv` values. | ||
26 | |||
27 | Shouldn't really need this, but due to `np_extra_opts()`, this is set after | ||
28 | that call. | ||
29 | |||
30 | ### np\_cleanup(void) | ||
31 | |||
32 | Used to clean up allocated memory by the `nagios_plugin` variable. This is | ||
33 | called by the `die()` routine before exiting. | ||
34 | |||
35 | ## State Information | ||
36 | |||
37 | Saving and restoring state allows a plugin to know the last results and thus | ||
38 | work out differences. This is especially useful when a plugin is capturing | ||
39 | counter information, which increases with every request. | ||
40 | |||
41 | This currently works by saving state information to a file, though the API | ||
42 | doesn't care what the backend implementation is. | ||
43 | |||
44 | *Note:* Binary data is not currently supported. | ||
45 | |||
46 | Some 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 | |||
61 | Enables the plugin state retention routines. Will set the filename for the | ||
62 | state file to be `.../{keyname}`. | ||
63 | |||
64 | The `keyname` will have any non alphanumerics replaced with "`_`". | ||
65 | |||
66 | If `keyname` is `NULL`, will generate an SHA1 `keyname` based on the `argv` of | ||
67 | the 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 | ||
70 | avoid a second invocation of the plugin to use the state information from a | ||
71 | different invocation. If in doubt, set `keyname=NULL` and allow the routine | ||
72 | to calculate the `keyname`. | ||
73 | |||
74 | ### np\_state\_read(void) | ||
75 | |||
76 | Reads the state file and returns a `state_data` variable. | ||
77 | |||
78 | This routine will call `die()` with `UNKNOWN` if: | ||
79 | |||
80 | * There was a problem reading the state file. | ||
81 | |||
82 | Returns `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 | |||
88 | Your plugin should always check for `NULL`. It is recommended that your | ||
89 | plugin returns `OK` on `NULL` as this is similar to a "first run". | ||
90 | |||
91 | If valid data was read, a pointer will be returned which points to a struct | ||
92 | of: | ||
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 | |||
102 | If `data_time==0`, use current time. Creates state file, with state format | ||
103 | version. Writes data version, time, and data. Creates a temporary file and | ||
104 | then renames into place. There is a possible loss of data if two things | ||
105 | writing to same key at same time, but there will not be a corrupted state | ||
106 | file. | ||
107 | |||
108 | ### np\_state\_write\_binary(time\_t data\_time, char \*start, int length) | ||
109 | |||
110 | Same as `np_state_write_string()`, but writes binary data. *Currently | ||
111 | unimplemented.* | ||
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: # %--> | ||