Skip to content

Instantly share code, notes, and snippets.

@YuriFontella
Last active January 23, 2019 12:08
Show Gist options
  • Save YuriFontella/338fb91bebe3612e31fc32870e12c43d to your computer and use it in GitHub Desktop.
Save YuriFontella/338fb91bebe3612e31fc32870e12c43d to your computer and use it in GitHub Desktop.
router.post('/zabbix_logic', async (req, res, next) => {
let alarm = { hostname: 'arch', group: 'servers', item: 'cpu', trigger: 'high', client: '5bbcab43c4ba0b3f9ba4eca6', type: 'zabbix' }
let current_time = moment().format('HH:mm:ss')
let rule = await model.Rules.findOne({
include: [{ as: 'acls', model: model.Acls }],
attributes: ['uuid', 'status'],
where: { client: alarm.client }
})
if (rule && rule.acls.length) {
let and = await model.Acls.findOne({
attributes: ['uuid', 'status', 'time_start', 'time_end'],
where: { type: alarm.type, rule_uuid: rule.uuid, [model.sequelize.Op.and]: [{ hostname: alarm.hostname }, { group: alarm.group }, { item: alarm.item }, { trigger: alarm.trigger }] },
order: [['updatedAt', 'DESC']]
})
if (and) {
console.log('caiu nas 4 condições')
if (and.status) {
console.log('o ticket vai ser aberto dependendo das condições de tempo')
if (current_time > and.time_start && current_time < and.time_end) {
console.log('sim!!')
} else {
console.log('não!!!')
}
} else {
console.log('o ticket não será aberto')
}
} else {
let or = await model.Acls.findOne({
attributes: ['uuid', 'hostname', 'group', 'item', 'trigger', 'status', 'time_start', 'time_end'],
where: { rule_uuid: rule.uuid, [model.sequelize.Op.or]: [{ hostname: alarm.hostname }, { group: alarm.group }, { item: alarm.item }, { trigger: alarm.trigger }] },
order: [['updatedAt', 'DESC']]
})
if (or) {
console.log('existe pelo menos uma ou mais condições')
console.log(JSON.stringify(or))
if (or.status) {
console.log('o ticket vai ser aberto dependendo das condições de tempo')
if (current_time > or.time_start && current_time < or.time_end) {
console.log('sim!!')
} else {
console.log('não!!!')
}
} else {
console.log('o ticket não será aberto')
}
} else {
console.log('realmente não existem condições, verifica se o cliente nega ou permite tudo')
if (rule.status) {
console.log('passou pelas regras e nada foi encontrado, mas o cliente permite tudo!')
} else {
console.log('passou pelas regras e nada foi encontrado, mas o cliente nega tudo!!')
}
}
}
} else if (rule && rule.status && !rule.acls.length) {
console.log('aqui permite tudo!!!')
} else {
console.log('aqui foi tudo negado!!!')
}
res.status(200).end()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment