cf6508e772eee7e7e9e6faf14b1ac37985dc0732
8 char const *cvs_id
= "$Id: throttlectl.c,v 1.1 2004-11-17 15:08:19 bodea Exp $";
10 int plugin_api_version
= PLUGIN_API_VERSION
;
11 static struct pluginfuncs
*p
= 0;
13 char *plugin_control_help
[] = {
14 " throttle USER|SID [RATE|[in|out] RATE ...] Throttle user traffic",
15 " unthrottle USER|SID Stop throttling user",
19 static int iam_master
= 0;
21 int plugin_init(struct pluginfuncs
*funcs
)
30 int plugin_become_master(void)
36 int plugin_control(struct param_control
*data
)
48 if (strcmp(data
->argv
[0], "throttle")
49 && strcmp(data
->argv
[0], "unthrottle"))
50 return PLUGIN_RET_OK
; // not for us
52 flag
= data
->argv
[0][0] != 'g';
56 data
->response
= NSCTL_RES_ERR
;
57 data
->additional
= "must be run on the cluster master";
58 return PLUGIN_RET_STOP
;
63 if (data
->argc
< 2 || data
->argc
> 4)
65 data
->response
= NSCTL_RES_ERR
;
66 data
->additional
= "requires username or session id and optional rate(s)";
67 return PLUGIN_RET_STOP
;
74 data
->response
= NSCTL_RES_ERR
;
75 data
->additional
= "requires username or session id";
76 return PLUGIN_RET_STOP
;
80 if (!(session
= strtol(data
->argv
[1], &end
, 10)) || *end
)
81 session
= p
->get_session_by_username(data
->argv
[1]);
84 s
= p
->get_session_by_id(session
);
88 data
->response
= NSCTL_RES_ERR
;
89 data
->additional
= "session not found";
90 return PLUGIN_RET_STOP
;
95 rate_in
= rate_out
= -1;
98 unsigned long *rate
= p
->getconfig("throttle_speed", UNSIGNED_LONG
);
99 rate_in
= rate_out
= *rate
;
101 else if (data
->argc
== 3)
103 rate_in
= rate_out
= atoi(data
->argv
[2]);
108 for (i
= 2; i
< data
->argc
- 1; i
+= 2)
110 int len
= strlen(data
->argv
[i
]);
111 if (!strncmp(data
->argv
[i
], "in", len
))
113 rate_in
= atoi(argv
[i
+1]);
115 else if (!strncmp(data
->argv
[i
], "out", len
))
117 rate_out
= atoi(argv
[i
+1]);
121 data
->response
= NSCTL_RES_ERR
;
122 data
->additional
= "invalid rate";
123 return PLUGIN_RET_STOP
;
128 if (!rate_in
|| !rate_out
)
130 data
->response
= NSCTL_RES_ERR
;
131 data
->additional
= "invalid rate";
132 return PLUGIN_RET_STOP
;
136 if (rate_in
!= -1 && rate_in
== s
->throttle_in
&&
137 rate_out
!= -1 && rate_out
== s
->throttle_out
)
139 data
->response
= NSCTL_RES_ERR
;
140 data
->additional
= flag
? "already throttled" : "not throttled";
141 return PLUGIN_RET_STOP
;
144 p
->throttle(session
, rate_in
, rate_out
);
145 p
->sesssion_changed(session
);
147 data
->response
= NSCTL_RES_OK
;
148 data
->additional
= 0;
150 return PLUGIN_RET_STOP
;