const LOCALE = undefined; const DATETIME_FORMAT = { year: "numeric", month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit", second: "2-digit" }; $(document).ready(function() { // Parse seconds ago to date // Get "now" timestamp var ts_now = Math.round((new Date()).getTime() / 1000); $('.parse_s_ago').each(function(i, parse_s_ago) { var started_s_ago = parseInt($(this).text(), 10); if (typeof started_s_ago != 'NaN') { var started_date = new Date((ts_now - started_s_ago) * 1000); if (started_date instanceof Date && !isNaN(started_date)) { var started_local_date = started_date.toLocaleDateString(LOCALE, DATETIME_FORMAT); $(this).text(started_local_date); } else { $(this).text('-'); } } }); // Parse general dates $('.parse_date').each(function(i, parse_date) { var started_date = new Date(Date.parse($(this).text())); if (typeof started_date != 'NaN') { var started_local_date = started_date.toLocaleDateString(LOCALE, DATETIME_FORMAT); $(this).text(started_local_date); } }); // set update loop container list containersToUpdate = {} // set default ChartJs Font Color Chart.defaults.color = '#999'; // create host cpu and mem charts createHostCpuAndMemChart(); // check for new version if (mailcow_info.branch === "master"){ check_update(mailcow_info.version_tag, mailcow_info.project_url); } $("#maiclow_version").click(function(){ if (mailcow_cc_role !== "admin" && mailcow_cc_role !== "domainadmin" || mailcow_info.branch !== "master") return; showVersionModal("Version " + mailcow_info.version_tag, mailcow_info.version_tag); }) // get public ips get_public_ips(); update_container_stats(); }); jQuery(function($){ if (localStorage.getItem("current_page") === null) { var current_page = {}; } else { var current_page = JSON.parse(localStorage.getItem('current_page')); } // http://stackoverflow.com/questions/24816/escaping-html-strings-with-jquery var entityMap={"&":"&","<":"<",">":">",'"':""","'":"'","/":"/","`":"`","=":"="}; function escapeHtml(n){return String(n).replace(/[&<>"'`=\/]/g,function(n){return entityMap[n]})} function humanFileSize(i){if(Math.abs(i)<1024)return i+" B";var B=["KiB","MiB","GiB","TiB","PiB","EiB","ZiB","YiB"],e=-1;do{i/=1024,++e}while(Math.abs(i)>=1024&&e 0 && item.symbols[a].score > 0) { return item.symbols[b].score - item.symbols[a].score } return item.symbols[b].score - item.symbols[a].score }).map(function(key) { var sym = item.symbols[key]; if (sym.score < 0) { sym.score_formatted = '(' + sym.score + ')' } else if (sym.score === 0) { sym.score_formatted = '(' + sym.score + ')' } else { sym.score_formatted = '(' + sym.score + ')' } var str = '' + key + ' ' + sym.score_formatted; if (sym.options) { str += ' [' + escapeHtml(sym.options.join(", ")) + "]"; } return str }).join('
\n'); item.subject = escapeHtml(item.subject); var scan_time = item.time_real.toFixed(3); if (item.time_virtual) { scan_time += ' / ' + item.time_virtual.toFixed(3); } item.scan_time = { "options": { "sortValue": item.time_real }, "value": scan_time }; if (item.action === 'clean' || item.action === 'no action') { item.action = "
" + item.action + "
"; } else if (item.action === 'rewrite subject' || item.action === 'add header' || item.action === 'probable spam') { item.action = "
" + item.action + "
"; } else if (item.action === 'spam' || item.action === 'reject') { item.action = "
" + item.action + "
"; } else { item.action = "
" + item.action + "
"; } var score_content; if (item.score < item.required_score) { score_content = "[ " + item.score.toFixed(2) + " / " + item.required_score + " ]"; } else { score_content = "[ " + item.score.toFixed(2) + " / " + item.required_score + " ]"; } item.score = { "options": { "sortValue": item.score }, "value": score_content }; if (item.user == null) { item.user = "none"; } }); } else if (table == 'autodiscover_log') { $.each(data, function (i, item) { if (item.ua == null) { item.ua = 'unknown'; } else { item.ua = escapeHtml(item.ua); } item.ua = '' + item.ua + ''; if (item.service == "activesync") { item.service = 'ActiveSync'; } else if (item.service == "imap") { item.service = 'IMAP, SMTP, Cal-/CardDAV'; } else { item.service = '' + escapeHtml(item.service) + ''; } }); } else if (table == 'watchdog') { $.each(data, function (i, item) { if (item.message == null) { item.message = 'Health level: ' + item.lvl + '% (' + item.hpnow + '/' + item.hptotal + ')'; if (item.hpdiff < 0) { item.trend = ' ' + item.hpdiff + ''; } else if (item.hpdiff == 0) { item.trend = ' ' + item.hpdiff + ''; } else { item.trend = ' ' + item.hpdiff + ''; } } else { item.trend = ''; item.service = ''; } }); } else if (table == 'mailcow_ui') { $.each(data, function (i, item) { if (item === null) { return true; } item.user = escapeHtml(item.user); item.call = escapeHtml(item.call); item.task = '' + item.task + ''; item.type = '' + item.type + ''; }); } else if (table == 'sasl_log_table') { $.each(data, function (i, item) { if (item === null) { return true; } item.username = escapeHtml(item.username); item.service = '
' + item.service.toUpperCase() + '
'; }); } else if (table == 'general_syslog') { $.each(data, function (i, item) { if (item === null) { return true; } if (item.message.match("^base64,")) { try { item.message = atob(item.message.slice(7)).replace(/\\n/g, "
"); } catch(e) { item.message = item.message.slice(7); } } else { item.message = escapeHtml(item.message); } item.call = escapeHtml(item.call); var danger_class = ["emerg", "alert", "crit", "err"]; var warning_class = ["warning", "warn"]; var info_class = ["notice", "info", "debug"]; if (jQuery.inArray(item.priority, danger_class) !== -1) { item.priority = '' + item.priority + ''; } else if (jQuery.inArray(item.priority, warning_class) !== -1) { item.priority = '' + item.priority + ''; } else if (jQuery.inArray(item.priority, info_class) !== -1) { item.priority = '' + item.priority + ''; } }); } else if (table == 'apilog') { $.each(data, function (i, item) { if (item === null) { return true; } if (item.method == 'GET') { item.method = '' + item.method + ''; } else if (item.method == 'POST') { item.method = '' + item.method + ''; } item.data = escapeHtml(item.data); }); } else if (table == 'rllog') { $.each(data, function (i, item) { if (item.user == null) { item.user = "none"; } if (item.rl_hash == null) { item.rl_hash = "err"; } item.indicator = ' '; if (item.rl_hash != 'err') { item.action = ' ' + lang.reset_limit + ''; } }); } return data }; $('.add_log_lines').on('click', function (e) { e.preventDefault(); var log_table= $(this).data("table") var new_nrows = $(this).data("nrows") var post_process = $(this).data("post-process") var log_url = $(this).data("log-url") if (log_table === undefined || new_nrows === undefined || post_process === undefined || log_url === undefined) { console.log("no data-table or data-nrows or log_url or data-post-process attr found"); return; } if (table = $('#' + log_table).DataTable()) { var heading = $('#' + log_table).closest('.card').find('.card-header'); var load_rows = (table.page.len() + 1) + '-' + (table.page.len() + new_nrows) $.get('/api/v1/get/logs/' + log_url + '/' + load_rows).then(function(data){ if (data.length === undefined) { mailcow_alert_box(lang.no_new_rows, "info"); return; } var rows = process_table_data(data, post_process); var rows_now = (table.page.len() + data.length); $(heading).children('.table-lines').text(rows_now) mailcow_alert_box(data.length + lang.additional_rows, "success"); table.rows.add(rows).draw(); }); } }) // detect element visibility changes function onVisible(element, callback) { $(document).ready(function() { element_object = document.querySelector(element); if (element_object === null) return; new IntersectionObserver((entries, observer) => { entries.forEach(entry => { if(entry.intersectionRatio > 0) { callback(element_object); } }); }).observe(element_object); }); } // Draw Table if tab is active onVisible("[id^=postfix_log]", () => draw_postfix_logs()); onVisible("[id^=dovecot_log]", () => draw_dovecot_logs()); onVisible("[id^=sogo_log]", () => draw_sogo_logs()); onVisible("[id^=watchdog_log]", () => draw_watchdog_logs()); onVisible("[id^=autodiscover_log]", () => draw_autodiscover_logs()); onVisible("[id^=acme_log]", () => draw_acme_logs()); onVisible("[id^=api_log]", () => draw_api_logs()); onVisible("[id^=rl_log]", () => draw_rl_logs()); onVisible("[id^=ui_logs]", () => draw_ui_logs()); onVisible("[id^=sasl_logs]", () => draw_sasl_logs()); onVisible("[id^=netfilter_log]", () => draw_netfilter_logs()); onVisible("[id^=rspamd_history]", () => draw_rspamd_history()); onVisible("[id^=rspamd_donut]", () => rspamd_pie_graph()); // start polling host stats if tab is active onVisible("[id^=tab-containers]", () => update_stats()); // start polling container stats if collapse is active var containerElements = document.querySelectorAll(".container-details-collapse"); for (let i = 0; i < containerElements.length; i++){ new IntersectionObserver((entries, observer) => { entries.forEach(entry => { if(entry.intersectionRatio > 0) { if (!containerElements[i].classList.contains("show")){ var container = containerElements[i].id.replace("Collapse", ""); var container_id = containerElements[i].getAttribute("data-id"); // check if chart exists or needs to be created if (!Chart.getChart(container + "_DiskIOChart")) createReadWriteChart(container + "_DiskIOChart", "Read", "Write"); if (!Chart.getChart(container + "_NetIOChart")) createReadWriteChart(container + "_NetIOChart", "Recv", "Sent"); // add container to polling list containersToUpdate[container] = { id: container_id, state: "idle" } // stop polling if collapse is closed containerElements[i].addEventListener('hidden.bs.collapse', function () { var diskIOCtx = Chart.getChart(container + "_DiskIOChart"); var netIOCtx = Chart.getChart(container + "_NetIOChart"); diskIOCtx.data.datasets[0].data = []; diskIOCtx.data.datasets[1].data = []; diskIOCtx.data.labels = []; netIOCtx.data.datasets[0].data = []; netIOCtx.data.datasets[1].data = []; netIOCtx.data.labels = []; diskIOCtx.update(); netIOCtx.update(); delete containersToUpdate[container]; }); } } }); }).observe(containerElements[i]); } }); // update system stats - every 5 seconds if system & container tab is active function update_stats(timeout=5){ if (!$('#tab-containers').hasClass('active')) { // tab not active - dont fetch stats - run again in n seconds return; } window.fetch("/api/v1/get/status/host", {method:'GET',cache:'no-cache'}).then(function(response) { return response.json(); }).then(function(data) { console.log(data); if (data){ // display table data $("#host_date").text(data.system_time); $("#host_uptime").text(formatUptime(data.uptime)); $("#host_cpu_cores").text(data.cpu.cores); $("#host_cpu_usage").text(parseInt(data.cpu.usage).toString() + "%"); $("#host_memory_total").text((data.memory.total / (1024 ** 3)).toFixed(2).toString() + "GB"); $("#host_memory_usage").text(parseInt(data.memory.usage).toString() + "%"); // update cpu and mem chart var cpu_chart = Chart.getChart("host_cpu_chart"); var mem_chart = Chart.getChart("host_mem_chart"); cpu_chart.data.labels.push(data.system_time.split(" ")[1]); if (cpu_chart.data.labels.length > 30) cpu_chart.data.labels.shift(); mem_chart.data.labels.push(data.system_time.split(" ")[1]); if (mem_chart.data.labels.length > 30) mem_chart.data.labels.shift(); cpu_chart.data.datasets[0].data.push(data.cpu.usage); if (cpu_chart.data.datasets[0].data.length > 30) cpu_chart.data.datasets[0].data.shift(); mem_chart.data.datasets[0].data.push(data.memory.usage); if (mem_chart.data.datasets[0].data.length > 30) mem_chart.data.datasets[0].data.shift(); cpu_chart.update(); mem_chart.update(); } // run again in n seconds setTimeout(update_stats, timeout * 1000); }); } // update specific container stats - every n (default 5s) seconds function update_container_stats(timeout=5){ if ($('#tab-containers').hasClass('active')) { for (let container in containersToUpdate){ container_id = containersToUpdate[container].id; // check if container update stats is already running if (containersToUpdate[container].state == "running") continue; containersToUpdate[container].state = "running"; window.fetch("/api/v1/get/status/container/" + container_id, {method:'GET',cache:'no-cache'}).then(function(response) { return response.json(); }).then(function(data) { var diskIOCtx = Chart.getChart(container + "_DiskIOChart"); var netIOCtx = Chart.getChart(container + "_NetIOChart"); console.log(container); console.log(data); prev_stats = null; if (data.length >= 2){ prev_stats = data[data.length -2]; // hide spinners if we collected enough data $('#' + container + "_DiskIOChart").removeClass('d-none'); $('#' + container + "_DiskIOChart").prev().addClass('d-none'); $('#' + container + "_NetIOChart").removeClass('d-none'); $('#' + container + "_NetIOChart").prev().addClass('d-none'); } data = data[data.length -1]; if (prev_stats != null){ // calc time diff var time_diff = (new Date(data.read) - new Date(prev_stats.read)) / 1000; // calc disk io b/s if ('io_service_bytes_recursive' in prev_stats.blkio_stats && prev_stats.blkio_stats.io_service_bytes_recursive !== null){ var prev_read_bytes = 0; var prev_write_bytes = 0; for (var i = 0; i < prev_stats.blkio_stats.io_service_bytes_recursive.length; i++){ if (prev_stats.blkio_stats.io_service_bytes_recursive[i].op == "read") prev_read_bytes = prev_stats.blkio_stats.io_service_bytes_recursive[i].value; else if (prev_stats.blkio_stats.io_service_bytes_recursive[i].op == "write") prev_write_bytes = prev_stats.blkio_stats.io_service_bytes_recursive[i].value; } var read_bytes = 0; var write_bytes = 0; for (var i = 0; i < data.blkio_stats.io_service_bytes_recursive.length; i++){ if (data.blkio_stats.io_service_bytes_recursive[i].op == "read") read_bytes = data.blkio_stats.io_service_bytes_recursive[i].value; else if (data.blkio_stats.io_service_bytes_recursive[i].op == "write") write_bytes = data.blkio_stats.io_service_bytes_recursive[i].value; } var diff_bytes_read = (read_bytes - prev_read_bytes) / time_diff; var diff_bytes_write = (write_bytes - prev_write_bytes) / time_diff; } // calc net io b/s if ('networks' in prev_stats){ var prev_recv_bytes = 0; var prev_sent_bytes = 0; for (var key in prev_stats.networks){ prev_recv_bytes += prev_stats.networks[key].rx_bytes; prev_sent_bytes += prev_stats.networks[key].tx_bytes; } var recv_bytes = 0; var sent_bytes = 0; for (var key in data.networks){ recv_bytes += data.networks[key].rx_bytes; sent_bytes += data.networks[key].tx_bytes; } var diff_bytes_recv = (recv_bytes - prev_recv_bytes) / time_diff; var diff_bytes_sent = (sent_bytes - prev_sent_bytes) / time_diff; } addReadWriteChart(diskIOCtx, diff_bytes_read, diff_bytes_write, ""); addReadWriteChart(netIOCtx, diff_bytes_recv, diff_bytes_sent, ""); } // run again in n seconds containersToUpdate[container].state = "idle"; }).catch(err => { console.log(err); }); } } // run again in n seconds setTimeout(update_container_stats, timeout * 1000); } // get public ips function get_public_ips(){ window.fetch("/api/v1/get/status/host/ip", {method:'GET',cache:'no-cache'}).then(function(response) { return response.json(); }).then(function(data) { console.log(data); // display host ips if (data.ipv4) $("#host_ipv4").text(data.ipv4); if (data.ipv6) $("#host_ipv6").text(data.ipv6); }); } // format hosts uptime seconds to readable string function formatUptime(seconds){ seconds = Number(seconds); var d = Math.floor(seconds / (3600*24)); var h = Math.floor(seconds % (3600*24) / 3600); var m = Math.floor(seconds % 3600 / 60); var s = Math.floor(seconds % 60); var dFormat = d > 0 ? d + "D " : ""; var hFormat = h > 0 ? h + "H " : ""; var mFormat = m > 0 ? m + "M " : ""; var sFormat = s > 0 ? s + "S" : ""; return dFormat + hFormat + mFormat + sFormat; } // format bytes to readable string function formatBytes(bytes){ // b if (bytes < 1000) return bytes.toFixed(2).toString()+' B/s'; // b to kb bytes = bytes / 1024; if (bytes < 1000) return bytes.toFixed(2).toString()+' KB/s'; // kb to mb bytes = bytes / 1024; if (bytes < 1000) return bytes.toFixed(2).toString()+' MB/s'; // final mb to gb return (bytes / 1024).toFixed(2).toString()+' GB/s'; } // create read write line chart function createReadWriteChart(chart_id, read_lable, write_lable){ var ctx = document.getElementById(chart_id); var dataNet = { labels: [], datasets: [{ label: read_lable, backgroundColor: "rgba(41, 187, 239, 0.3)", borderColor: "rgba(41, 187, 239, 0.6)", pointRadius: 1, pointHitRadius: 6, borderWidth: 2, fill: true, tension: 0.2, data: [] }, { label: write_lable, backgroundColor: "rgba(239, 60, 41, 0.3)", borderColor: "rgba(239, 60, 41, 0.6)", pointRadius: 1, pointHitRadius: 6, borderWidth: 2, fill: true, tension: 0.2, data: [] }] }; var optionsNet = { interaction: { mode: 'index' }, scales: { yAxis: { min: 0, grid: { display: false }, ticks: { callback: function(i, index, ticks) { return formatBytes(i); } } }, xAxis: { grid: { display: false } } } }; return new Chart(ctx, { type: 'line', data: dataNet, options: optionsNet }); } // add to read write line chart function addReadWriteChart(chart_context, read_point, write_point, time, limit = 30){ // push time label for x-axis chart_context.data.labels.push(time); if (chart_context.data.labels.length > limit) chart_context.data.labels.shift(); // push datapoints chart_context.data.datasets[0].data.push(read_point); chart_context.data.datasets[1].data.push(write_point); // shift data if more than 20 entires exists if (chart_context.data.datasets[0].data.length > limit) chart_context.data.datasets[0].data.shift(); if (chart_context.data.datasets[1].data.length > limit) chart_context.data.datasets[1].data.shift(); chart_context.update(); } // create host cpu and mem chart function createHostCpuAndMemChart(){ var cpu_ctx = document.getElementById("host_cpu_chart"); var mem_ctx = document.getElementById("host_mem_chart"); var dataCpu = { labels: [], datasets: [{ label: "CPU %", backgroundColor: "rgba(41, 187, 239, 0.3)", borderColor: "rgba(41, 187, 239, 0.6)", pointRadius: 1, pointHitRadius: 6, borderWidth: 2, fill: true, tension: 0.2, data: [] }] }; var optionsCpu = { interaction: { mode: 'index' }, scales: { yAxis: { min: 0, grid: { display: false }, ticks: { callback: function(i, index, ticks) { return i.toFixed(0).toString() + "%"; } } }, xAxis: { grid: { display: false } } } }; var dataMem = { labels: [], datasets: [{ label: "MEM %", backgroundColor: "rgba(41, 187, 239, 0.3)", borderColor: "rgba(41, 187, 239, 0.6)", pointRadius: 1, pointHitRadius: 6, borderWidth: 2, fill: true, tension: 0.2, data: [] }] }; var optionsMem = { interaction: { mode: 'index' }, scales: { yAxis: { min: 0, grid: { display: false }, ticks: { callback: function(i, index, ticks) { return i.toFixed(0).toString() + "%"; } } }, xAxis: { grid: { display: false } } } }; var net_io_chart = new Chart(cpu_ctx, { type: 'line', data: dataCpu, options: optionsCpu }); var disk_io_chart = new Chart(mem_ctx, { type: 'line', data: dataMem, options: optionsMem }); } // check for mailcow updates function check_update(current_version, github_repo_url){ if (!current_version || !github_repo_url) return false; var github_account = github_repo_url.split("/")[3]; var github_repo_name = github_repo_url.split("/")[4]; // get details about latest release window.fetch("https://api.github.com/repos/"+github_account+"/"+github_repo_name+"/releases/latest", {method:'GET',cache:'no-cache'}).then(function(response) { return response.json(); }).then(function(latest_data) { // get details about current release window.fetch("https://api.github.com/repos/"+github_account+"/"+github_repo_name+"/releases/tags/"+current_version, {method:'GET',cache:'no-cache'}).then(function(response) { return response.json(); }).then(function(current_data) { // compare releases var date_current = new Date(current_data.created_at); var date_latest = new Date(latest_data.created_at); if (date_latest.getTime() <= date_current.getTime()){ // no update available $("#mailcow_update").removeClass("text-warning text-danger").addClass("text-success"); $("#mailcow_update").html("" + lang_debug.no_update_available + ""); } else { // update available $("#mailcow_update").removeClass("text-danger text-success").addClass("text-warning"); $("#mailcow_update").html(lang_debug.update_available + ` `+latest_data.tag_name+``); $("#mailcow_update_changelog").click(function(){ if (mailcow_cc_role !== "admin" && mailcow_cc_role !== "domainadmin") return; showVersionModal("New Release " + latest_data.tag_name, latest_data.tag_name); }) } }).catch(err => { // err console.log(err); $("#mailcow_update").removeClass("text-success text-warning").addClass("text-danger"); $("#mailcow_update").html(""+ lang_debug.update_failed +""); }); }).catch(err => { // err console.log(err); $("#mailcow_update").removeClass("text-success text-warning").addClass("text-danger"); $("#mailcow_update").html(""+ lang_debug.update_failed +""); }); } // show version changelog modal function showVersionModal(title, version){ $.ajax({ type: 'GET', url: 'https://api.github.com/repos/' + mailcow_info.project_owner + '/' + mailcow_info.project_repo + '/releases/tags/' + version, dataType: 'json', success: function (data) { var md = window.markdownit(); var result = md.render(data.body); result = parseGithubMarkdownLinks(result); $('#showVersionModal').find(".modal-title").html(title); $('#showVersionModal').find(".modal-body").html(`

` + data.name + `

` + result + ` Github Link: ` + version + ` `); new bootstrap.Modal(document.getElementById("showVersionModal"), { backdrop: 'static', keyboard: false }).show(); } }); } function parseGithubMarkdownLinks(inputText) { var replacedText, replacePattern1; replacePattern1 = /(\b(https?):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim; replacedText = inputText.replace(replacePattern1, (matched, index, original, input_string) => { if (matched.includes('github.com')){ // return short link if it's github link last_uri_path = matched.split('/'); last_uri_path = last_uri_path[last_uri_path.length - 1]; // adjust Full Changelog link to match last git version and new git version, if link is a compare link if (matched.includes('/compare/') && mailcow_info.last_version_tag !== ''){ matched = matched.replace(last_uri_path, mailcow_info.last_version_tag + '...' + mailcow_info.version_tag); last_uri_path = mailcow_info.last_version_tag + '...' + mailcow_info.version_tag; } return '' + last_uri_path + '
'; }; // if it's not a github link, return complete link return '' + matched + ''; }); return replacedText; } function convertTimestampToLocalFormat(timestamp) { var date = new Date(timestamp ? timestamp * 1000 : 0); return date.toLocaleDateString(LOCALE, DATETIME_FORMAT); }