1UPSCLI_GET(3) NUT Manual UPSCLI_GET(3)
2
3
4
6 upscli_get - retrieve data from an UPS
7
9 #include <upsclient.h>
10
11 int upscli_get(UPSCONN_t *ups, size_t numq, const char **query,
12 size_t *numa, char ***answer)
13
15 The upscli_get() function takes the pointer ups to a UPSCONN_t state
16 structure, and the pointer query to an array of numq query elements. It
17 builds a properly-formatted request from those elements and transmits
18 it to upsd(8).
19
20 Upon success, the response will be split into separate components. A
21 pointer to those components will be returned in answer. The number of
22 usable answer components will be returned in numa.
23
25 This function implements the "GET" command in the protocol. As a
26 result, you can use it to request many different things from the
27 server.
28
29 Some examples are:
30
31 • GET NUMLOGINS <ups>
32
33 • GET UPSDESC <ups>
34
35 • GET VAR <ups> <var>
36
37 • GET TYPE <ups> <var>
38
39 • GET DESC <ups> <var>
40
41 • GET CMDDESC <ups> <cmd>
42
44 To generate a request for GET NUMLOGINS su700, you would populate query
45 and numq as follows:
46
47 size_t numq;
48 const char *query[2];
49
50 query[0] = "NUMLOGINS";
51 query[1] = "su700";
52 numq = 2;
53
54 All escaping of special characters and quoting of elements with spaces
55 is handled for you inside this function.
56
58 The raw response from upsd to the above query would be NUMLOGINS su700
59 1.
60
61 Since this is split up for you, the values work out like this:
62
63 size_t numa;
64
65 numa = 3;
66 answer[0] = "NUMLOGINS"
67 answer[1] = "su700"
68 answer[2] = "1"
69
70 Notice that the value which you seek typically starts at answer[numq].
71
73 This function will check your query against the response from upsd(8).
74 For example, if you send "VAR" "su700" "ups.status", it will expect to
75 see those at the beginning of the response.
76
77 If the results from upsd do not pass this case-insensitive test against
78 your request, this function will return an error. When this happens,
79 upscli_upserror(3) will return UPSCLI_ERR_PROTOCOL.
80
82 The pointers contained within the answer array are only valid until the
83 next call to a upsclient function which references them. If you need to
84 use data from multiple calls, you must copy it somewhere else first.
85
86 The answer array and its elements may change locations, so you must not
87 rely on previous addresses. You must only use the addresses which were
88 returned by the most recent call. You also must not attempt to use more
89 than numa elements in answer. Such behavior is undefined, and may yield
90 bogus data or a crash.
91
92 The array will be deleted after calling upscli_disconnect(3). Any
93 access after that point is also undefined.
94
96 The upscli_get() function returns 0 on success, or -1 if an error
97 occurs.
98
99 If upsd disconnects, you may need to handle or ignore SIGPIPE in order
100 to prevent your program from terminating the next time that the library
101 writes to the disconnected socket.
102
103 The following code in your initialization function will allow the
104 upscli_get() call to return an error in that case:
105
106 #include <signal.h>
107 ...
108 signal (SIGPIPE, SIG_IGN);
109 ...
110
112 upscli_list_start(3), upscli_list_next(3), upscli_strerror(3),
113 upscli_upserror(3)
114
115
116
117Network UPS Tools 2.8.0 04/26/2022 UPSCLI_GET(3)