<commit_before>require_relative '../../IRCPlugin'

class Memory < IRCPlugin
  Description = "Monitors memory useage."
  Commands = {
    :free => "returns free memory"
  }

  def on_privmsg(msg)
    case msg.botcommand
      when :free
        free_array = `free`.split(" ")
        mfree = free_array[8].to_i
        mtotal = free_array[7].to_i
        sfree = free_array[19].to_i
        stotal = free_array[18].to_i
        msg.reply "Memory usage: #{'%.2f' % (mfree / 1024)} of #{'%.2f' % (mtotal / 1024)} MiB \/\/ SWAP usage: #{'%.2f' % (sfree / 1024)} of #{'%.2f' % (stotal / 1024)} MiB"
    end
  end
end
<commit_msg>Read /proc/meminfo directly in Memory<commit_after>require_relative '../../IRCPlugin'

class Memory < IRCPlugin
  Description = 'Monitors memory usage.'
  Commands = {
    :free => 'reports memory info'
  }

  def on_privmsg(msg)
    case msg.botcommand
      when :free
        d = File.open('/proc/meminfo') do |f|
          Hash[f.each_line.map do |l|
            key, value = l.split()
            [key.downcase.delete(':').to_sym, value.to_i]
          end]
        end

        # Thanks to CalimeroTeknik for this memory calculation formula
        mused = d[:memtotal] - d[:memfree] - d[:cached] - d[:buffers] + d[:shmem] + d[:slab] + d[:kernelstack] + d[:pagetables]
        mtotal = d[:memtotal]

        stotal = d[:swaptotal]
        sused = stotal - d[:swapfree]

        msg.reply "Memory usage: #{'%.2f' % (mused / 1024)} of #{'%.2f' % (mtotal / 1024)} MiB \/\/ SWAP usage: #{'%.2f' % (sused / 1024)} of #{'%.2f' % (stotal / 1024)} MiB"
    end
  end
end

{'fraction_non_alphanumeric': 0.16760828625235405, 'fraction_numerical': 0.028876333961079723, 'mean_word_length': 2.3628691983122363, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3280885', 'n_tokens_mistral': 609, 'n_tokens_neox': 568, 'n_words': 140}