ipmi_sensor.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  1. /*
  2. * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * Redistribution of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * Redistribution in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * Neither the name of Sun Microsystems, Inc. or the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * This software is provided "AS IS," without a warranty of any kind.
  20. * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
  21. * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
  22. * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.
  23. * SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE
  24. * FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  25. * OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
  26. * SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA,
  27. * OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
  28. * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
  29. * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
  30. * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  31. */
  32. #include <string.h>
  33. #include <math.h>
  34. #include <ipmitool/ipmi.h>
  35. #include <ipmitool/helper.h>
  36. #include <ipmitool/log.h>
  37. #include <ipmitool/ipmi_intf.h>
  38. #include <ipmitool/ipmi_sdr.h>
  39. #include <ipmitool/ipmi_sel.h>
  40. #include <ipmitool/ipmi_sensor.h>
  41. extern int verbose;
  42. void print_sensor_get_usage();
  43. void print_sensor_thresh_usage();
  44. // Macro's for Reading the current sensor Data.
  45. #define SCANNING_DISABLED 0x40
  46. #define READING_UNAVAILABLE 0x20
  47. #define INVALID_THRESHOLD "Invalid Threshold data values. Cannot Set Threshold Data."
  48. // static
  49. int
  50. ipmi_sensor_get_sensor_reading_factors(
  51. struct ipmi_intf * intf,
  52. struct sdr_record_full_sensor * sensor,
  53. uint8_t reading)
  54. {
  55. struct ipmi_rq req;
  56. struct ipmi_rs * rsp;
  57. uint8_t req_data[2];
  58. char id[17];
  59. if (intf == NULL || sensor == NULL)
  60. return -1;
  61. memset(id, 0, sizeof(id));
  62. memcpy(id, sensor->id_string, 16);
  63. req_data[0] = sensor->cmn.keys.sensor_num;
  64. req_data[1] = reading;
  65. memset(&req, 0, sizeof(req));
  66. req.msg.netfn = IPMI_NETFN_SE;
  67. req.msg.lun = sensor->cmn.keys.lun;
  68. req.msg.cmd = GET_SENSOR_FACTORS;
  69. req.msg.data = req_data;
  70. req.msg.data_len = sizeof(req_data);
  71. rsp = intf->sendrecv(intf, &req);
  72. if (rsp == NULL) {
  73. lprintf(LOG_ERR, "Error updating reading factor for sensor %s (#%02x)",
  74. id, sensor->cmn.keys.sensor_num);
  75. return -1;
  76. } else if (rsp->ccode) {
  77. return -1;
  78. } else {
  79. /* Update SDR copy with updated Reading Factors for this reading */
  80. /* Note:
  81. * The Format of the returned data is exactly as in the SDR definition (Little Endian Format),
  82. * therefore we can use raw copy operation here.
  83. * Note: rsp->data[0] would point to the next valid entry in the sampling table
  84. */
  85. // BUGBUG: uses 'hardcoded' length information from SDR Definition
  86. memcpy(&sensor->mtol, &rsp->data[1], sizeof(sensor->mtol));
  87. memcpy(&sensor->bacc, &rsp->data[3], sizeof(sensor->bacc));
  88. return 0;
  89. }
  90. }
  91. static
  92. struct ipmi_rs *
  93. ipmi_sensor_set_sensor_thresholds(struct ipmi_intf *intf,
  94. uint8_t sensor,
  95. uint8_t threshold, uint8_t setting,
  96. uint8_t target, uint8_t lun, uint8_t channel)
  97. {
  98. struct ipmi_rq req;
  99. static struct sensor_set_thresh_rq set_thresh_rq;
  100. struct ipmi_rs *rsp;
  101. uint8_t bridged_request = 0;
  102. uint32_t save_addr;
  103. uint32_t save_channel;
  104. memset(&set_thresh_rq, 0, sizeof (set_thresh_rq));
  105. set_thresh_rq.sensor_num = sensor;
  106. set_thresh_rq.set_mask = threshold;
  107. if (threshold == UPPER_NON_RECOV_SPECIFIED)
  108. set_thresh_rq.upper_non_recov = setting;
  109. else if (threshold == UPPER_CRIT_SPECIFIED)
  110. set_thresh_rq.upper_crit = setting;
  111. else if (threshold == UPPER_NON_CRIT_SPECIFIED)
  112. set_thresh_rq.upper_non_crit = setting;
  113. else if (threshold == LOWER_NON_CRIT_SPECIFIED)
  114. set_thresh_rq.lower_non_crit = setting;
  115. else if (threshold == LOWER_CRIT_SPECIFIED)
  116. set_thresh_rq.lower_crit = setting;
  117. else if (threshold == LOWER_NON_RECOV_SPECIFIED)
  118. set_thresh_rq.lower_non_recov = setting;
  119. else
  120. return NULL;
  121. if (BRIDGE_TO_SENSOR(intf, target, channel)) {
  122. bridged_request = 1;
  123. save_addr = intf->target_addr;
  124. intf->target_addr = target;
  125. save_channel = intf->target_channel;
  126. intf->target_channel = channel;
  127. }
  128. memset(&req, 0, sizeof (req));
  129. req.msg.netfn = IPMI_NETFN_SE;
  130. req.msg.lun = lun;
  131. req.msg.cmd = SET_SENSOR_THRESHOLDS;
  132. req.msg.data = (uint8_t *) & set_thresh_rq;
  133. req.msg.data_len = sizeof (set_thresh_rq);
  134. rsp = intf->sendrecv(intf, &req);
  135. if (bridged_request) {
  136. intf->target_addr = save_addr;
  137. intf->target_channel = save_channel;
  138. }
  139. return rsp;
  140. }
  141. static int
  142. ipmi_sensor_print_fc_discrete(struct ipmi_intf *intf,
  143. struct sdr_record_common_sensor *sensor,
  144. uint8_t sdr_record_type)
  145. {
  146. struct sensor_reading *sr;
  147. sr = ipmi_sdr_read_sensor_value(intf, sensor, sdr_record_type, 3);
  148. if (sr == NULL) {
  149. return -1;
  150. }
  151. if (csv_output) {
  152. /* NOT IMPLEMENTED */
  153. } else {
  154. if (verbose == 0) {
  155. /* output format
  156. * id value units status thresholds....
  157. */
  158. printf("%-16s ", sr->s_id);
  159. if (sr->s_reading_valid) {
  160. if (sr->s_has_analog_value) {
  161. /* don't show discrete component */
  162. printf("| %-10s | %-10s | %-6s",
  163. sr->s_a_str, sr->s_a_units, "ok");
  164. } else {
  165. printf("| 0x%-8x | %-10s | 0x%02x%02x",
  166. sr->s_reading, "discrete",
  167. sr->s_data2, sr->s_data3);
  168. }
  169. } else {
  170. printf("| %-10s | %-10s | %-6s",
  171. "na", "discrete", "na");
  172. }
  173. printf("| %-10s| %-10s| %-10s| %-10s| %-10s| %-10s",
  174. "na", "na", "na", "na", "na", "na");
  175. printf("\n");
  176. } else {
  177. printf("Sensor ID : %s (0x%x)\n",
  178. sr->s_id, sensor->keys.sensor_num);
  179. printf(" Entity ID : %d.%d\n",
  180. sensor->entity.id, sensor->entity.instance);
  181. printf(" Sensor Type (Discrete): %s\n",
  182. ipmi_get_sensor_type(intf, sensor->sensor.
  183. type));
  184. if( sr->s_reading_valid )
  185. {
  186. if (sr->s_has_analog_value) {
  187. printf(" Sensor Reading : %s %s\n", sr->s_a_str, sr->s_a_units);
  188. }
  189. ipmi_sdr_print_discrete_state(intf, "States Asserted",
  190. sensor->sensor.type,
  191. sensor->event_type,
  192. sr->s_data2,
  193. sr->s_data3);
  194. printf("\n");
  195. } else {
  196. printf(" Unable to read sensor: Device Not Present\n\n");
  197. }
  198. }
  199. }
  200. return (sr->s_reading_valid ? 0 : -1 );
  201. }
  202. static void
  203. print_thresh_setting(struct sdr_record_full_sensor *full,
  204. uint8_t thresh_is_avail, uint8_t setting,
  205. const char *field_sep,
  206. const char *analog_fmt,
  207. const char *discrete_fmt,
  208. const char *na_fmt)
  209. {
  210. printf("%s", field_sep);
  211. if (!thresh_is_avail) {
  212. printf(na_fmt, "na");
  213. return;
  214. }
  215. if (full && !UNITS_ARE_DISCRETE(&full->cmn)) {
  216. printf(analog_fmt, sdr_convert_sensor_reading (full, setting));
  217. } else {
  218. printf(discrete_fmt, setting);
  219. }
  220. }
  221. static int
  222. ipmi_sensor_print_fc_threshold(struct ipmi_intf *intf,
  223. struct sdr_record_common_sensor *sensor,
  224. uint8_t sdr_record_type)
  225. {
  226. int thresh_available = 1;
  227. struct ipmi_rs *rsp;
  228. struct sensor_reading *sr;
  229. sr = ipmi_sdr_read_sensor_value(intf, sensor, sdr_record_type, 3);
  230. if (sr == NULL) {
  231. return -1;
  232. }
  233. const char *thresh_status = ipmi_sdr_get_thresh_status(sr, "ns");
  234. /*
  235. * Get sensor thresholds
  236. */
  237. rsp = ipmi_sdr_get_sensor_thresholds(intf,
  238. sensor->keys.sensor_num, sensor->keys.owner_id,
  239. sensor->keys.lun, sensor->keys.channel);
  240. if ((rsp == NULL) || (rsp->ccode > 0) || (rsp->data_len == 0))
  241. thresh_available = 0;
  242. if (csv_output) {
  243. /* NOT IMPLEMENTED */
  244. } else {
  245. if (verbose == 0) {
  246. /* output format
  247. * id value units status thresholds....
  248. */
  249. printf("%-16s ", sr->s_id);
  250. if (sr->s_reading_valid) {
  251. if (sr->s_has_analog_value)
  252. printf("| %-10.3f | %-10s | %-6s",
  253. sr->s_a_val, sr->s_a_units, thresh_status);
  254. else
  255. printf("| 0x%-8x | %-10s | %-6s",
  256. sr->s_reading, sr->s_a_units, thresh_status);
  257. } else {
  258. printf("| %-10s | %-10s | %-6s",
  259. "na", sr->s_a_units, "na");
  260. }
  261. if (thresh_available && sr->full) {
  262. #define PTS(bit, dataidx) { \
  263. print_thresh_setting(sr->full, rsp->data[0] & (bit), \
  264. rsp->data[(dataidx)], "| ", "%-10.3f", "0x-8x", "%-10s"); \
  265. }
  266. PTS(LOWER_NON_RECOV_SPECIFIED, 3);
  267. PTS(LOWER_CRIT_SPECIFIED, 2);
  268. PTS(LOWER_NON_CRIT_SPECIFIED, 1);
  269. PTS(UPPER_NON_CRIT_SPECIFIED, 4);
  270. PTS(UPPER_CRIT_SPECIFIED, 5);
  271. PTS(UPPER_NON_RECOV_SPECIFIED, 6);
  272. #undef PTS
  273. } else {
  274. printf
  275. ("| %-10s| %-10s| %-10s| %-10s| %-10s| %-10s",
  276. "na", "na", "na", "na", "na", "na");
  277. }
  278. printf("\n");
  279. } else {
  280. printf("Sensor ID : %s (0x%x)\n",
  281. sr->s_id, sensor->keys.sensor_num);
  282. printf(" Entity ID : %d.%d\n",
  283. sensor->entity.id, sensor->entity.instance);
  284. printf(" Sensor Type (Threshold) : %s\n",
  285. ipmi_get_sensor_type(intf, sensor->sensor.
  286. type));
  287. printf(" Sensor Reading : ");
  288. if (sr->s_reading_valid) {
  289. if (sr->full) {
  290. uint16_t raw_tol = __TO_TOL(sr->full->mtol);
  291. if (sr->s_has_analog_value) {
  292. double tol =
  293. sdr_convert_sensor_tolerance(sr->full,
  294. raw_tol);
  295. printf("%.*f (+/- %.*f) %s\n",
  296. (sr->s_a_val == (int)
  297. sr->s_a_val) ? 0 : 3,
  298. sr->s_a_val,
  299. (tol == (int) tol) ? 0 : 3, tol,
  300. sr->s_a_units);
  301. } else {
  302. printf("0x%x (+/- 0x%x) %s\n",
  303. sr->s_reading,
  304. raw_tol,
  305. sr->s_a_units);
  306. }
  307. } else {
  308. printf("0x%x %s\n", sr->s_reading,
  309. sr->s_a_units);
  310. }
  311. printf(" Status : %s\n", thresh_status);
  312. if (thresh_available) {
  313. if (sr->full) {
  314. #define PTS(bit, dataidx, str) { \
  315. print_thresh_setting(sr->full, rsp->data[0] & (bit), \
  316. rsp->data[(dataidx)], \
  317. (str), "%.3f\n", "0x%x\n", "%s\n"); \
  318. }
  319. PTS(LOWER_NON_RECOV_SPECIFIED, 3, " Lower Non-Recoverable : ");
  320. PTS(LOWER_CRIT_SPECIFIED, 2, " Lower Critical : ");
  321. PTS(LOWER_NON_CRIT_SPECIFIED, 1, " Lower Non-Critical : ");
  322. PTS(UPPER_NON_CRIT_SPECIFIED, 4, " Upper Non-Critical : ");
  323. PTS(UPPER_CRIT_SPECIFIED, 5, " Upper Critical : ");
  324. PTS(UPPER_NON_RECOV_SPECIFIED, 6, " Upper Non-Recoverable : ");
  325. #undef PTS
  326. }
  327. ipmi_sdr_print_sensor_hysteresis(sensor, sr->full,
  328. sr->full ? sr->full->threshold.hysteresis.positive :
  329. sr->compact->threshold.hysteresis.positive,
  330. "Positive Hysteresis");
  331. ipmi_sdr_print_sensor_hysteresis(sensor, sr->full,
  332. sr->full ? sr->full->threshold.hysteresis.negative :
  333. sr->compact->threshold.hysteresis.negative,
  334. "Negative Hysteresis");
  335. } else {
  336. printf(" Sensor Threshold Settings not available\n");
  337. }
  338. } else {
  339. printf(" Unable to read sensor: Device Not Present\n\n");
  340. }
  341. ipmi_sdr_print_sensor_event_status(intf,
  342. sensor->keys.
  343. sensor_num,
  344. sensor->sensor.type,
  345. sensor->event_type,
  346. ANALOG_SENSOR,
  347. sensor->keys.owner_id,
  348. sensor->keys.lun,
  349. sensor->keys.channel);
  350. ipmi_sdr_print_sensor_event_enable(intf,
  351. sensor->keys.
  352. sensor_num,
  353. sensor->sensor.type,
  354. sensor->event_type,
  355. ANALOG_SENSOR,
  356. sensor->keys.owner_id,
  357. sensor->keys.lun,
  358. sensor->keys.channel);
  359. printf("\n");
  360. }
  361. }
  362. return (sr->s_reading_valid ? 0 : -1 );
  363. }
  364. int
  365. ipmi_sensor_print_fc(struct ipmi_intf *intf,
  366. struct sdr_record_common_sensor *sensor,
  367. uint8_t sdr_record_type)
  368. {
  369. if (IS_THRESHOLD_SENSOR(sensor))
  370. return ipmi_sensor_print_fc_threshold(intf, sensor, sdr_record_type);
  371. else
  372. return ipmi_sensor_print_fc_discrete(intf, sensor, sdr_record_type);
  373. }
  374. static int
  375. ipmi_sensor_list(struct ipmi_intf *intf)
  376. {
  377. struct sdr_get_rs *header;
  378. struct ipmi_sdr_iterator *itr;
  379. int rc = 0;
  380. lprintf(LOG_DEBUG, "Querying SDR for sensor list");
  381. itr = ipmi_sdr_start(intf, 0);
  382. if (itr == NULL) {
  383. lprintf(LOG_ERR, "Unable to open SDR for reading");
  384. return -1;
  385. }
  386. while ((header = ipmi_sdr_get_next_header(intf, itr)) != NULL) {
  387. uint8_t *rec;
  388. rec = ipmi_sdr_get_record(intf, header, itr);
  389. if (rec == NULL) {
  390. lprintf(LOG_DEBUG, "rec == NULL");
  391. continue;
  392. }
  393. switch (header->type) {
  394. case SDR_RECORD_TYPE_FULL_SENSOR:
  395. case SDR_RECORD_TYPE_COMPACT_SENSOR:
  396. ipmi_sensor_print_fc(intf,
  397. (struct
  398. sdr_record_common_sensor *)
  399. rec,
  400. header->type);
  401. break;
  402. }
  403. free(rec);
  404. rec = NULL;
  405. /* fix for CR6604909: */
  406. /* mask failure of individual reads in sensor list command */
  407. /* rc = (r == 0) ? rc : r; */
  408. }
  409. ipmi_sdr_end(intf, itr);
  410. return rc;
  411. }
  412. static const struct valstr threshold_vals[] = {
  413. {UPPER_NON_RECOV_SPECIFIED, "Upper Non-Recoverable"},
  414. {UPPER_CRIT_SPECIFIED, "Upper Critical"},
  415. {UPPER_NON_CRIT_SPECIFIED, "Upper Non-Critical"},
  416. {LOWER_NON_RECOV_SPECIFIED, "Lower Non-Recoverable"},
  417. {LOWER_CRIT_SPECIFIED, "Lower Critical"},
  418. {LOWER_NON_CRIT_SPECIFIED, "Lower Non-Critical"},
  419. {0x00, NULL},
  420. };
  421. static int
  422. __ipmi_sensor_set_threshold(struct ipmi_intf *intf,
  423. uint8_t num, uint8_t mask, uint8_t setting,
  424. uint8_t target, uint8_t lun, uint8_t channel)
  425. {
  426. struct ipmi_rs *rsp;
  427. rsp = ipmi_sensor_set_sensor_thresholds(intf, num, mask, setting,
  428. target, lun, channel);
  429. if (rsp == NULL) {
  430. lprintf(LOG_ERR, "Error setting threshold");
  431. return -1;
  432. }
  433. if (rsp->ccode > 0) {
  434. lprintf(LOG_ERR, "Error setting threshold: %s",
  435. val2str(rsp->ccode, completion_code_vals));
  436. return -1;
  437. }
  438. return 0;
  439. }
  440. static uint8_t
  441. __ipmi_sensor_threshold_value_to_raw(struct sdr_record_full_sensor *full, double value)
  442. {
  443. if (!UNITS_ARE_DISCRETE(&full->cmn)) { /* Has an analog reading */
  444. /* Has an analog reading and supports mx+b */
  445. return sdr_convert_sensor_value_to_raw(full, value);
  446. }
  447. else {
  448. /* Does not have an analog reading and/or does not support mx+b */
  449. if (value > 255) {
  450. return 255;
  451. }
  452. else if (value < 0) {
  453. return 0;
  454. }
  455. else {
  456. return (uint8_t )value;
  457. }
  458. }
  459. }
  460. static int
  461. ipmi_sensor_set_threshold(struct ipmi_intf *intf, int argc, char **argv)
  462. {
  463. char *id, *thresh;
  464. uint8_t settingMask = 0;
  465. double setting1 = 0.0, setting2 = 0.0, setting3 = 0.0;
  466. int allUpper = 0, allLower = 0;
  467. int ret = 0;
  468. struct ipmi_rs *rsp;
  469. int i =0;
  470. double val[10] = {0};
  471. struct sdr_record_list *sdr;
  472. if (argc < 3 || strncmp(argv[0], "help", 4) == 0) {
  473. print_sensor_thresh_usage();
  474. return 0;
  475. }
  476. id = argv[0];
  477. thresh = argv[1];
  478. if (strncmp(thresh, "upper", 5) == 0) {
  479. if (argc < 5) {
  480. lprintf(LOG_ERR,
  481. "usage: sensor thresh <id> upper <unc> <ucr> <unr>");
  482. return -1;
  483. }
  484. allUpper = 1;
  485. if (str2double(argv[2], &setting1) != 0) {
  486. lprintf(LOG_ERR, "Given unc '%s' is invalid.",
  487. argv[2]);
  488. return (-1);
  489. }
  490. if (str2double(argv[3], &setting2) != 0) {
  491. lprintf(LOG_ERR, "Given ucr '%s' is invalid.",
  492. argv[3]);
  493. return (-1);
  494. }
  495. if (str2double(argv[4], &setting3) != 0) {
  496. lprintf(LOG_ERR, "Given unr '%s' is invalid.",
  497. argv[4]);
  498. return (-1);
  499. }
  500. } else if (strncmp(thresh, "lower", 5) == 0) {
  501. if (argc < 5) {
  502. lprintf(LOG_ERR,
  503. "usage: sensor thresh <id> lower <lnr> <lcr> <lnc>");
  504. return -1;
  505. }
  506. allLower = 1;
  507. if (str2double(argv[2], &setting1) != 0) {
  508. lprintf(LOG_ERR, "Given lnc '%s' is invalid.",
  509. argv[2]);
  510. return (-1);
  511. }
  512. if (str2double(argv[3], &setting2) != 0) {
  513. lprintf(LOG_ERR, "Given lcr '%s' is invalid.",
  514. argv[3]);
  515. return (-1);
  516. }
  517. if (str2double(argv[4], &setting3) != 0) {
  518. lprintf(LOG_ERR, "Given lnr '%s' is invalid.",
  519. argv[4]);
  520. return (-1);
  521. }
  522. } else {
  523. if (strncmp(thresh, "unr", 3) == 0)
  524. settingMask = UPPER_NON_RECOV_SPECIFIED;
  525. else if (strncmp(thresh, "ucr", 3) == 0)
  526. settingMask = UPPER_CRIT_SPECIFIED;
  527. else if (strncmp(thresh, "unc", 3) == 0)
  528. settingMask = UPPER_NON_CRIT_SPECIFIED;
  529. else if (strncmp(thresh, "lnc", 3) == 0)
  530. settingMask = LOWER_NON_CRIT_SPECIFIED;
  531. else if (strncmp(thresh, "lcr", 3) == 0)
  532. settingMask = LOWER_CRIT_SPECIFIED;
  533. else if (strncmp(thresh, "lnr", 3) == 0)
  534. settingMask = LOWER_NON_RECOV_SPECIFIED;
  535. else {
  536. lprintf(LOG_ERR,
  537. "Valid threshold '%s' for sensor '%s' not specified!",
  538. thresh, id);
  539. return -1;
  540. }
  541. if (str2double(argv[2], &setting1) != 0) {
  542. lprintf(LOG_ERR,
  543. "Given %s threshold value '%s' is invalid.",
  544. thresh, argv[2]);
  545. return (-1);
  546. }
  547. }
  548. printf("Locating sensor record '%s'...\n", id);
  549. /* lookup by sensor name */
  550. sdr = ipmi_sdr_find_sdr_byid(intf, id);
  551. if (sdr == NULL) {
  552. lprintf(LOG_ERR, "Sensor data record not found!");
  553. return -1;
  554. }
  555. if (sdr->type != SDR_RECORD_TYPE_FULL_SENSOR) {
  556. lprintf(LOG_ERR, "Invalid sensor type %02x", sdr->type);
  557. return -1;
  558. }
  559. if (!IS_THRESHOLD_SENSOR(sdr->record.common)) {
  560. lprintf(LOG_ERR, "Invalid sensor event type %02x", sdr->record.common->event_type);
  561. return -1;
  562. }
  563. if (allUpper) {
  564. settingMask = UPPER_NON_CRIT_SPECIFIED;
  565. printf("Setting sensor \"%s\" %s threshold to %.3f\n",
  566. sdr->record.full->id_string,
  567. val2str(settingMask, threshold_vals), setting1);
  568. ret = __ipmi_sensor_set_threshold(intf,
  569. sdr->record.common->keys.
  570. sensor_num, settingMask,
  571. __ipmi_sensor_threshold_value_to_raw(sdr->record.full, setting1),
  572. sdr->record.common->keys.owner_id,
  573. sdr->record.common->keys.lun,
  574. sdr->record.common->keys.channel);
  575. settingMask = UPPER_CRIT_SPECIFIED;
  576. printf("Setting sensor \"%s\" %s threshold to %.3f\n",
  577. sdr->record.full->id_string,
  578. val2str(settingMask, threshold_vals), setting2);
  579. ret = __ipmi_sensor_set_threshold(intf,
  580. sdr->record.common->keys.
  581. sensor_num, settingMask,
  582. __ipmi_sensor_threshold_value_to_raw(sdr->record.full, setting2),
  583. sdr->record.common->keys.owner_id,
  584. sdr->record.common->keys.lun,
  585. sdr->record.common->keys.channel);
  586. settingMask = UPPER_NON_RECOV_SPECIFIED;
  587. printf("Setting sensor \"%s\" %s threshold to %.3f\n",
  588. sdr->record.full->id_string,
  589. val2str(settingMask, threshold_vals), setting3);
  590. ret = __ipmi_sensor_set_threshold(intf,
  591. sdr->record.common->keys.
  592. sensor_num, settingMask,
  593. __ipmi_sensor_threshold_value_to_raw(sdr->record.full, setting3),
  594. sdr->record.common->keys.owner_id,
  595. sdr->record.common->keys.lun,
  596. sdr->record.common->keys.channel);
  597. } else if (allLower) {
  598. settingMask = LOWER_NON_RECOV_SPECIFIED;
  599. printf("Setting sensor \"%s\" %s threshold to %.3f\n",
  600. sdr->record.full->id_string,
  601. val2str(settingMask, threshold_vals), setting1);
  602. ret = __ipmi_sensor_set_threshold(intf,
  603. sdr->record.common->keys.
  604. sensor_num, settingMask,
  605. __ipmi_sensor_threshold_value_to_raw(sdr->record.full, setting1),
  606. sdr->record.common->keys.owner_id,
  607. sdr->record.common->keys.lun,
  608. sdr->record.common->keys.channel);
  609. settingMask = LOWER_CRIT_SPECIFIED;
  610. printf("Setting sensor \"%s\" %s threshold to %.3f\n",
  611. sdr->record.full->id_string,
  612. val2str(settingMask, threshold_vals), setting2);
  613. ret = __ipmi_sensor_set_threshold(intf,
  614. sdr->record.common->keys.
  615. sensor_num, settingMask,
  616. __ipmi_sensor_threshold_value_to_raw(sdr->record.full, setting2),
  617. sdr->record.common->keys.owner_id,
  618. sdr->record.common->keys.lun,
  619. sdr->record.common->keys.channel);
  620. settingMask = LOWER_NON_CRIT_SPECIFIED;
  621. printf("Setting sensor \"%s\" %s threshold to %.3f\n",
  622. sdr->record.full->id_string,
  623. val2str(settingMask, threshold_vals), setting3);
  624. ret = __ipmi_sensor_set_threshold(intf,
  625. sdr->record.common->keys.
  626. sensor_num, settingMask,
  627. __ipmi_sensor_threshold_value_to_raw(sdr->record.full, setting3),
  628. sdr->record.common->keys.owner_id,
  629. sdr->record.common->keys.lun,
  630. sdr->record.common->keys.channel);
  631. } else {
  632. /*
  633. * Current implementation doesn't check for the valid setting of upper non critical and other thresholds.
  634. * In the below logic:
  635. * Get all the current reading of the sensor i.e. unc, uc, lc,lnc.
  636. * Validate the values given by the user.
  637. * If the values are not correct, then popup with the Error message and return.
  638. */
  639. /*
  640. * Get current reading
  641. */
  642. rsp = ipmi_sdr_get_sensor_reading_ipmb(intf,
  643. sdr->record.common->keys.sensor_num,
  644. sdr->record.common->keys.owner_id,
  645. sdr->record.common->keys.lun,sdr->record.common->keys.channel);
  646. rsp = ipmi_sdr_get_sensor_thresholds(intf,
  647. sdr->record.common->keys.sensor_num,
  648. sdr->record.common->keys.owner_id,
  649. sdr->record.common->keys.lun,
  650. sdr->record.common->keys.channel);
  651. if ((rsp == NULL) || (rsp->ccode > 0)) {
  652. lprintf(LOG_ERR, "Sensor data record not found!");
  653. return -1;
  654. }
  655. for(i=1;i<=6;i++) {
  656. val[i] = sdr_convert_sensor_reading(sdr->record.full, rsp->data[i]);
  657. if(val[i] < 0)
  658. val[i] = 0;
  659. }
  660. /* Check for the valid Upper non recovarable Value.*/
  661. if( (settingMask & UPPER_NON_RECOV_SPECIFIED) ) {
  662. if( (rsp->data[0] & UPPER_NON_RECOV_SPECIFIED) &&
  663. (( (rsp->data[0] & UPPER_CRIT_SPECIFIED) && ( setting1 <= val[5])) ||
  664. ( (rsp->data[0] & UPPER_NON_CRIT_SPECIFIED) && ( setting1 <= val[4]))) )
  665. {
  666. lprintf(LOG_ERR, INVALID_THRESHOLD);
  667. return -1;
  668. }
  669. } else if( (settingMask & UPPER_CRIT_SPECIFIED) ) { /* Check for the valid Upper critical Value.*/
  670. if( (rsp->data[0] & UPPER_CRIT_SPECIFIED) &&
  671. (((rsp->data[0] & UPPER_NON_RECOV_SPECIFIED)&& ( setting1 >= val[6])) ||
  672. ((rsp->data[0] & UPPER_NON_CRIT_SPECIFIED)&&( setting1 <= val[4]))) )
  673. {
  674. lprintf(LOG_ERR, INVALID_THRESHOLD);
  675. return -1;
  676. }
  677. } else if( (settingMask & UPPER_NON_CRIT_SPECIFIED) ) { /* Check for the valid Upper non critical Value.*/
  678. if( (rsp->data[0] & UPPER_NON_CRIT_SPECIFIED) &&
  679. (((rsp->data[0] & UPPER_NON_RECOV_SPECIFIED)&&( setting1 >= val[6])) ||
  680. ((rsp->data[0] & UPPER_CRIT_SPECIFIED)&&( setting1 >= val[5])) ||
  681. ((rsp->data[0] & LOWER_NON_CRIT_SPECIFIED)&&( setting1 <= val[1]))) )
  682. {
  683. lprintf(LOG_ERR, INVALID_THRESHOLD);
  684. return -1;
  685. }
  686. } else if( (settingMask & LOWER_NON_CRIT_SPECIFIED) ) { /* Check for the valid lower non critical Value.*/
  687. if( (rsp->data[0] & LOWER_NON_CRIT_SPECIFIED) &&
  688. (((rsp->data[0] & LOWER_CRIT_SPECIFIED)&&( setting1 <= val[2])) ||
  689. ((rsp->data[0] & LOWER_NON_RECOV_SPECIFIED)&&( setting1 <= val[3]))||
  690. ((rsp->data[0] & UPPER_NON_CRIT_SPECIFIED)&&( setting1 >= val[4]))) )
  691. {
  692. lprintf(LOG_ERR, INVALID_THRESHOLD);
  693. return -1;
  694. }
  695. } else if( (settingMask & LOWER_CRIT_SPECIFIED) ) { /* Check for the valid lower critical Value.*/
  696. if( (rsp->data[0] & LOWER_CRIT_SPECIFIED) &&
  697. (((rsp->data[0] & LOWER_NON_CRIT_SPECIFIED)&&( setting1 >= val[1])) ||
  698. ((rsp->data[0] & LOWER_NON_RECOV_SPECIFIED)&&( setting1 <= val[3]))) )
  699. {
  700. lprintf(LOG_ERR, INVALID_THRESHOLD);
  701. return -1;
  702. }
  703. } else if( (settingMask & LOWER_NON_RECOV_SPECIFIED) ) { /* Check for the valid lower non recovarable Value.*/
  704. if( (rsp->data[0] & LOWER_NON_RECOV_SPECIFIED) &&
  705. (((rsp->data[0] & LOWER_NON_CRIT_SPECIFIED)&&( setting1 >= val[1])) ||
  706. ((rsp->data[0] & LOWER_CRIT_SPECIFIED)&&( setting1 >= val[2]))) )
  707. {
  708. lprintf(LOG_ERR, INVALID_THRESHOLD);
  709. return -1;
  710. }
  711. } else { /* None of this Then Return with error messages.*/
  712. lprintf(LOG_ERR, INVALID_THRESHOLD);
  713. return -1;
  714. }
  715. printf("Setting sensor \"%s\" %s threshold to %.3f\n",
  716. sdr->record.full->id_string,
  717. val2str(settingMask, threshold_vals), setting1);
  718. ret = __ipmi_sensor_set_threshold(intf,
  719. sdr->record.common->keys.
  720. sensor_num, settingMask,
  721. __ipmi_sensor_threshold_value_to_raw(sdr->record.full, setting1),
  722. sdr->record.common->keys.owner_id,
  723. sdr->record.common->keys.lun,
  724. sdr->record.common->keys.channel);
  725. }
  726. return ret;
  727. }
  728. static int
  729. ipmi_sensor_get_reading(struct ipmi_intf *intf, int argc, char **argv)
  730. {
  731. struct sdr_record_list *sdr;
  732. int i, rc=0;
  733. if (argc < 1 || strncmp(argv[0], "help", 4) == 0) {
  734. lprintf(LOG_NOTICE, "sensor reading <id> ... [id]");
  735. lprintf(LOG_NOTICE, " id : name of desired sensor");
  736. return -1;
  737. }
  738. for (i = 0; i < argc; i++) {
  739. sdr = ipmi_sdr_find_sdr_byid(intf, argv[i]);
  740. if (sdr == NULL) {
  741. lprintf(LOG_ERR, "Sensor \"%s\" not found!",
  742. argv[i]);
  743. rc = -1;
  744. continue;
  745. }
  746. switch (sdr->type) {
  747. case SDR_RECORD_TYPE_FULL_SENSOR:
  748. case SDR_RECORD_TYPE_COMPACT_SENSOR:
  749. {
  750. struct sensor_reading *sr;
  751. struct sdr_record_common_sensor *sensor = sdr->record.common;
  752. sr = ipmi_sdr_read_sensor_value(intf, sensor, sdr->type, 3);
  753. if (sr == NULL) {
  754. rc = -1;
  755. continue;
  756. }
  757. if (!sr->full)
  758. continue;
  759. if (!sr->s_reading_valid)
  760. continue;
  761. if (!sr->s_has_analog_value) {
  762. lprintf(LOG_ERR, "Sensor \"%s\" is a discrete sensor!", argv[i]);
  763. continue;
  764. }
  765. if (csv_output)
  766. printf("%s,%s\n", argv[i], sr->s_a_str);
  767. else
  768. printf("%-16s | %s\n", argv[i], sr->s_a_str);
  769. break;
  770. }
  771. default:
  772. continue;
  773. }
  774. }
  775. return rc;
  776. }
  777. static int
  778. ipmi_sensor_get(struct ipmi_intf *intf, int argc, char **argv)
  779. {
  780. int i, v;
  781. int rc = 0;
  782. struct sdr_record_list *sdr;
  783. if (argc < 1) {
  784. lprintf(LOG_ERR, "Not enough parameters given.");
  785. print_sensor_get_usage();
  786. return (-1);
  787. } else if (strcmp(argv[0], "help") == 0) {
  788. print_sensor_get_usage();
  789. return 0;
  790. }
  791. printf("Locating sensor record...\n");
  792. /* lookup by sensor name */
  793. for (i = 0; i < argc; i++) {
  794. sdr = ipmi_sdr_find_sdr_byid(intf, argv[i]);
  795. if (sdr == NULL) {
  796. lprintf(LOG_ERR, "Sensor data record \"%s\" not found!",
  797. argv[i]);
  798. rc = -1;
  799. continue;
  800. }
  801. /* need to set verbose level to 1 */
  802. v = verbose;
  803. verbose = 1;
  804. switch (sdr->type) {
  805. case SDR_RECORD_TYPE_FULL_SENSOR:
  806. case SDR_RECORD_TYPE_COMPACT_SENSOR:
  807. if (ipmi_sensor_print_fc(intf,
  808. (struct sdr_record_common_sensor *) sdr->record.common,
  809. sdr->type)) {
  810. rc = -1;
  811. }
  812. break;
  813. default:
  814. if (ipmi_sdr_print_listentry(intf, sdr) < 0) {
  815. rc = (-1);
  816. }
  817. break;
  818. }
  819. verbose = v;
  820. sdr = NULL;
  821. }
  822. return rc;
  823. }
  824. int
  825. ipmi_sensor_main(struct ipmi_intf *intf, int argc, char **argv)
  826. {
  827. int rc = 0;
  828. if (argc == 0) {
  829. rc = ipmi_sensor_list(intf);
  830. } else if (strncmp(argv[0], "help", 4) == 0) {
  831. lprintf(LOG_NOTICE, "Sensor Commands: list thresh get reading");
  832. } else if (strncmp(argv[0], "list", 4) == 0) {
  833. rc = ipmi_sensor_list(intf);
  834. } else if (strncmp(argv[0], "thresh", 5) == 0) {
  835. rc = ipmi_sensor_set_threshold(intf, argc - 1, &argv[1]);
  836. } else if (strncmp(argv[0], "get", 3) == 0) {
  837. rc = ipmi_sensor_get(intf, argc - 1, &argv[1]);
  838. } else if (strncmp(argv[0], "reading", 7) == 0) {
  839. rc = ipmi_sensor_get_reading(intf, argc - 1, &argv[1]);
  840. } else {
  841. lprintf(LOG_ERR, "Invalid sensor command: %s", argv[0]);
  842. rc = -1;
  843. }
  844. return rc;
  845. }
  846. /* print_sensor_get_usage - print usage for # ipmitool sensor get NAC;
  847. *
  848. * @returns: void
  849. */
  850. void
  851. print_sensor_get_usage()
  852. {
  853. lprintf(LOG_NOTICE, "sensor get <id> ... [id]");
  854. lprintf(LOG_NOTICE, " id : name of desired sensor");
  855. }
  856. /* print_sensor_thresh_set_usage - print usage for # ipmitool sensor thresh;
  857. *
  858. * @returns: void
  859. */
  860. void
  861. print_sensor_thresh_usage()
  862. {
  863. lprintf(LOG_NOTICE,
  864. "sensor thresh <id> <threshold> <setting>");
  865. lprintf(LOG_NOTICE,
  866. " id : name of the sensor for which threshold is to be set");
  867. lprintf(LOG_NOTICE,
  868. " threshold : which threshold to set");
  869. lprintf(LOG_NOTICE,
  870. " unr = upper non-recoverable");
  871. lprintf(LOG_NOTICE,
  872. " ucr = upper critical");
  873. lprintf(LOG_NOTICE,
  874. " unc = upper non-critical");
  875. lprintf(LOG_NOTICE,
  876. " lnc = lower non-critical");
  877. lprintf(LOG_NOTICE,
  878. " lcr = lower critical");
  879. lprintf(LOG_NOTICE,
  880. " lnr = lower non-recoverable");
  881. lprintf(LOG_NOTICE,
  882. " setting : the value to set the threshold to");
  883. lprintf(LOG_NOTICE,
  884. "");
  885. lprintf(LOG_NOTICE,
  886. "sensor thresh <id> lower <lnr> <lcr> <lnc>");
  887. lprintf(LOG_NOTICE,
  888. " Set all lower thresholds at the same time");
  889. lprintf(LOG_NOTICE,
  890. "");
  891. lprintf(LOG_NOTICE,
  892. "sensor thresh <id> upper <unc> <ucr> <unr>");
  893. lprintf(LOG_NOTICE,
  894. " Set all upper thresholds at the same time");
  895. lprintf(LOG_NOTICE, "");
  896. }