2 * Copyright (C) 2020 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: MIT
8 #include <lttng/condition/event-rule.h>
9 #include <lttng/lttng.h>
19 bool action_group_contains_notify(const struct lttng_action
*action_group
)
21 unsigned int i
, count
;
22 enum lttng_action_status status
=
23 lttng_action_group_get_count(action_group
, &count
);
25 if (status
!= LTTNG_ACTION_STATUS_OK
) {
26 printf("Failed to get action count from action group\n");
30 for (i
= 0; i
< count
; i
++) {
31 const struct lttng_action
*action
=
32 lttng_action_group_get_at_index_const(
34 const enum lttng_action_type action_type
=
35 lttng_action_get_type(action
);
37 if (action_type
== LTTNG_ACTION_TYPE_NOTIFY
) {
44 int print_notification(struct lttng_notification
*notification
)
47 const struct lttng_condition
*condition
=
48 lttng_notification_get_condition(notification
);
49 const struct lttng_evaluation
*evaluation
=
50 lttng_notification_get_evaluation(notification
);
51 const enum lttng_condition_type type
=
52 lttng_evaluation_get_type(evaluation
);
55 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
56 printf("Received consumed size notification\n");
58 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
59 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
60 printf("Received buffer usage notification\n");
62 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
63 printf("Received session rotation ongoing notification\n");
65 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
66 printf("Received session rotation completed notification\n");
68 case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
:
70 const char *trigger_name
;
71 enum lttng_evaluation_status evaluation_status
;
76 gettimeofday(&tv
, NULL
);
79 strftime(time_str
, sizeof(time_str
), "[%m-%d-%Y] %T",
80 localtime(&the_time
));
81 printf("%s.%ld - ", time_str
, tv
.tv_usec
);
84 lttng_evaluation_event_rule_get_trigger_name(
85 evaluation
, &trigger_name
);
86 if (evaluation_status
!= LTTNG_EVALUATION_STATUS_OK
) {
87 fprintf(stderr
, "Failed to get trigger name of event rule notification\n");
92 printf("Received notification of event rule trigger \"%s\"\n",
97 fprintf(stderr
, "Unknown notification type (%d)\n", type
);
103 int main(int argc
, char **argv
)
106 struct lttng_triggers
*triggers
= NULL
;
107 unsigned int count
, i
, subcription_count
= 0;
108 enum lttng_trigger_status trigger_status
;
109 struct lttng_notification_channel
*notification_channel
= NULL
;
112 fprintf(stderr
, "Missing trigger name\n");
113 fprintf(stderr
, "Usage: notification-client TRIGGER_NAME\n");
117 notification_channel
= lttng_notification_channel_create(
118 lttng_session_daemon_notification_endpoint
);
119 if (!notification_channel
) {
120 fprintf(stderr
, "Failed to create notification channel\n");
125 ret
= lttng_list_triggers(&triggers
);
127 fprintf(stderr
, "Failed to list triggers\n");
131 trigger_status
= lttng_triggers_get_count(triggers
, &count
);
132 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
133 fprintf(stderr
, "Failed to get trigger count\n");
138 for (i
= 0; i
< count
; i
++) {
139 const struct lttng_trigger
*trigger
=
140 lttng_triggers_get_at_index(triggers
, i
);
141 const struct lttng_condition
*condition
=
142 lttng_trigger_get_const_condition(trigger
);
143 const struct lttng_action
*action
=
144 lttng_trigger_get_const_action(trigger
);
145 const enum lttng_action_type action_type
=
146 lttng_action_get_type(action
);
147 enum lttng_notification_channel_status channel_status
;
148 const char *trigger_name
= NULL
;
149 enum lttng_trigger_status trigger_status
;
151 lttng_trigger_get_name(trigger
, &trigger_name
);
152 if (strcmp(trigger_name
, argv
[1])) {
156 if (!((action_type
== LTTNG_ACTION_TYPE_GROUP
&&
157 action_group_contains_notify(action
)) ||
158 action_type
== LTTNG_ACTION_TYPE_NOTIFY
)) {
159 printf("The action of trigger \"%s\" is not \"notify\", skipping.\n",
164 channel_status
= lttng_notification_channel_subscribe(
165 notification_channel
, condition
);
166 if (channel_status
) {
167 fprintf(stderr
, "Failed to subscribe to notifications of trigger \"%s\"\n",
173 printf("Subscribed to notifications of trigger \"%s\"\n",
178 if (subcription_count
== 0) {
179 printf("No matching trigger with a notify action found.\n");
185 struct lttng_notification
*notification
;
186 enum lttng_notification_channel_status channel_status
;
189 lttng_notification_channel_get_next_notification(
190 notification_channel
,
192 switch (channel_status
) {
193 case LTTNG_NOTIFICATION_CHANNEL_STATUS_NOTIFICATIONS_DROPPED
:
194 printf("Dropped notification\n");
196 case LTTNG_NOTIFICATION_CHANNEL_STATUS_INTERRUPTED
:
199 case LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
:
201 case LTTNG_NOTIFICATION_CHANNEL_STATUS_CLOSED
:
202 printf("Notification channel was closed by peer.\n");
205 fprintf(stderr
, "A communication error occurred on the notification channel.\n");
210 ret
= print_notification(notification
);
211 lttng_notification_destroy(notification
);
217 lttng_triggers_destroy(triggers
);
218 lttng_notification_channel_destroy(notification_channel
);