Skip to content

Commit

Permalink
Allow msp resync alarm
Browse files Browse the repository at this point in the history
  • Loading branch information
xinnige committed Jul 18, 2024
1 parent 7eaa108 commit 0a7212d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
36 changes: 28 additions & 8 deletions alarm/AlarmManager2.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ module.exports = class {
}
case Constants.ST_ACTIVATED:
case Constants.ST_IGNORE: {
// delete immediately
await rclient.zremAsync(alarmPendingKey, aid);
break;
}
Expand Down Expand Up @@ -341,7 +340,11 @@ module.exports = class {
if (options.origin && options.origin.state == Constants.ST_IGNORE){
return
}
await rclient.hsetAsync(alarmPrefix + alarmID, 'p.msp.decision', 'ignore');
let mspDec = 'ignore';
if (options.origin && options.origin['p.msp.decision']) {
mspDec = options.origin['p.msp.decision'] + ',' + mspDec;
}
await rclient.hsetAsync(alarmPrefix + alarmID, 'p.msp.decision', mspDec);
await this.archiveAlarm(alarmID);
await rclient.zremAsync(alarmPendingKey, alarmID);
}
Expand Down Expand Up @@ -550,7 +553,7 @@ module.exports = class {
if (alarmConfig.hasOwnProperty(alias)) {
alarm.apply(alarmConfig[alias]);
} else if (alarmConfig.default){ // default
alarm.apply(alarmConfig.default);
alarm.apply(_.omit(alarmConfig.default, ['timeout']));
}
}

Expand All @@ -568,6 +571,12 @@ module.exports = class {
}
log.debug('apply alarm attrs', alarm, 'to', orig_alarm);
let attrs = {state: orig_alarm.state}; // origin attrs
if (orig_alarm.hasOwnProperty('p.msp.decision')) {
attrs['p.msp.decision'] = orig_alarm['p.msp.decision'];
}

// only allow reapply state: ignore -> ready or active -> ignore
let redecision = (orig_alarm.state == Constants.ST_ACTIVATED && alarm.state == Constants.ST_IGNORE) || ( orig_alarm.state == Constants.ST_IGNORE && alarm.state == Constants.ST_READY);
for (const k in alarm) {
if (alarm[k] != orig_alarm[k]) {
attrs[k] = orig_alarm[k];
Expand All @@ -577,7 +586,7 @@ module.exports = class {
delete alarm[k];
continue;
}
if (k == "state" && alarm[k] != orig_alarm.state && (orig_alarm.state == Constants.ST_ACTIVATED || orig_alarm.state == Constants.ST_IGNORE)) {
if (k == "state" && alarm[k] != orig_alarm.state && (orig_alarm.state == Constants.ST_ACTIVATED || orig_alarm.state == Constants.ST_IGNORE) && !redecision) {
log.warn('alarm already activated or ignored, skip change state', alarm);
delete alarm[k];
continue
Expand All @@ -597,7 +606,11 @@ module.exports = class {
async _onState(alarm, options={}) {
switch (alarm.state) {
case Constants.ST_READY: {
options['p.msp.decision'] = 'active';
if (options.origin['p.msp.decision']) {
options['p.msp.decision'] = options.origin['p.msp.decision'] + ',active';
} else {
options['p.msp.decision'] = 'active';
}
await this.activateAlarm(alarm, options);
break;
}
Expand All @@ -618,7 +631,7 @@ module.exports = class {
for (const attr in attrs) {
switch (attr) {
case 'state': {
const opt = Object.assign({}, options, {state: attrs.state});
const opt = Object.assign({}, options, {origin:attrs});
await this._onState(alarm, opt);
break;
}
Expand Down Expand Up @@ -753,8 +766,11 @@ module.exports = class {
return alarmID
}

async _activateAlarm(alarm) {
async _activateAlarm(alarm, unarchive = false) {
let score = parseFloat(alarm.alarmTimestamp) || new Date() / 1000;
if (unarchive) {
await rclient.zremAsync(alarmArchiveKey, alarm.aid);
}
return await rclient.multi()
.zrem(alarmPendingKey, alarm.aid)
.zadd(alarmActiveKey, 'NX', score, alarm.aid)
Expand All @@ -763,6 +779,7 @@ module.exports = class {

async activateAlarm(alarm, options={}) {
log.info('activate alarm', alarm, options);
let unarchive = false;

if (this.isAlarmSyncMspEnabled()) {
if ((alarm.state && alarm.state == Constants.ST_ACTIVATED) || (options.origin && options.origin.state == Constants.ST_ACTIVATED)) {
Expand All @@ -774,6 +791,9 @@ module.exports = class {
log.debug(`alarm ${alarm.aid} still pending`)
return;
}
if (alarm.state && alarm.state == Constants.ST_READY && options.origin && options.origin.state == Constants.ST_IGNORE) {
unarchive = true
}
}

alarm.state = Constants.ST_ACTIVATED;
Expand All @@ -786,7 +806,7 @@ module.exports = class {

const orig_alarm = await rclient.hgetallAsync(alarmKey);
alarm = Object.assign({}, orig_alarm, alarm);
const result = await this._activateAlarm(alarm);
const result = await this._activateAlarm(alarm, unarchive);

// check alarm state change results
if (this.isAlarmSyncMspEnabled() && result.length >= 2) {
Expand Down
2 changes: 1 addition & 1 deletion locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"INFO_ALARM_DEVICE_BACK_ONLINE": "{{{p.device.name}}} is back to your network",
"NOTIF_TITLE_ALARM_DEVICE_OFFLINE": "Device Offline",
"NOTIF_ALARM_DEVICE_OFFLINE": "{{{p.device.name}}} has left your network at {{{p.device.lastSeenTimezone}}}",
"INFO_ALARM_DEVICE_OFFLINE": "{{{p.device.name}}} is back to your network",
"INFO_ALARM_DEVICE_OFFLINE": "{{{p.device.name}}} has left your network at {{{p.device.lastSeenTimezone}}}",
"NOTIF_TITLE_ALARM_SPOOFING_DEVICE": "Suspicious Spoofing Device Detected",
"NOTIF_ALARM_SPOOFING_DEVICE": "Suspicious spoofing device found: {{{p.device.name}}} ({{{p.device.ip}}})",
"INFO_ALARM_SPOOFING_DEVICE": "Found suspicious device {{{p.device.name}}} in your network",
Expand Down
17 changes: 13 additions & 4 deletions tests/test_alarm_manager2.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,17 @@ const am2 = new AlarmManager2();
describe('Test alarm event', function(){
this.timeout(30000);

beforeEach((done) => {
before((done) => {
(
async() => {
await fc.syncDynamicFeatures();
await fc.getConfig(true);
}
)();
done();
});

afterEach((done) => {
after((done) => {
done();
});

Expand Down Expand Up @@ -123,16 +124,24 @@ describe('Test alarm event', function(){
describe('Test AlarmManager2', function(){
this.timeout(30000);

beforeEach((done) => {
before((done) => {
(
async() => {
await fc.syncDynamicFeatures();
this.extdata = await rclient.getAsync("ext.guardian.data");
await rclient.setAsync("ext.guardian.data", "{\"config\":{\"alarms\":{\"apply\":{\"default\":{\"state\":\"ready\",\"timeout\":1800},\"large_upload\":{\"state\":\"pending\"},\"large_upload_2\":{\"state\":\"pending\"}}}}}");
log.debug("fc.getConfig", await fc.getConfig(true));
}
)();
done();
});

afterEach((done) => {
after((done) => {
(
async() => {
await rclient.setAsync("ext.guardian.data", this.extdata);
}
)();
done();
});

Expand Down

0 comments on commit 0a7212d

Please sign in to comment.